【问题标题】:Selenium Xpath how do i get the value of an html id tag using starts-withSelenium Xpath我如何使用starts-with获取html id标签的值
【发布时间】:2016-03-10 22:05:59
【问题描述】:

我有一个带有 id 属性的 div 标签的 html。我想在 Xpath 中使用starts-with 来获取id 属性的值。

这是一个 HTML sn-p:

<div id="operations_edit_process_list_task_3">
    <span/>
<span>
<span class=" myinlineblock" title="Clean"
      style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;">
<select tabindex="-1">
</span>
</span>

使用 Xpath 开头我想将 id 值放入一个变量中,以便稍后在我的代码中使用它。

末尾的数字3是动态的,如果我可以使用starts-with那么我可以得到id值。

我尝试了以下 Xpath,它不起作用:

//div[starts-with[@div="operations_edit_process_list_task"]]

什么是正确的语法? 谢谢, 里亚兹

【问题讨论】:

    标签: python-2.7 selenium xpath selenium-webdriver


    【解决方案1】:

    我不知道如何使用xpath,但使用css_selector 你可以做到

    element = driver.find_element_by_css_selector("[id^='operations_edit_process_list_task']")
    id = element.get_attribute("id")
    # do whatever you want with the id value
    

    我认为xpath 的正确语法是

    //*[starts-with(div, "operations_edit_process_list_task")]
    

    【讨论】:

    • 我可以将 id 变量的值打印到控制台吗?我试过打印 id.text 我得到一个错误 'unicode' object has no attribute 'text'
    • 我也试过 print id.get_attribute("id") 我得到错误 'unicode' object has no attribute 'get_attribute'
    • @RiazLadhani id 是字符串,它具有 element.get_attribute("id") 返回的值。
    • 只要print id,你应该得到"operations_edit_process_list_task_3"
    【解决方案2】:

    要查找具有 XPath 且 id 以 : 开头的元素:

    "//*[starts-with(@id,'beginning of id')]"
    

    使用 CSS 选择器查找 id 以 : 开头的元素:

    "[id^='beginning of id']"
    

    【讨论】:

      猜你喜欢
      • 2014-12-09
      • 2016-12-04
      • 2021-03-31
      • 1970-01-01
      • 2016-11-08
      • 1970-01-01
      • 2014-06-17
      • 1970-01-01
      • 2013-10-14
      相关资源
      最近更新 更多