【问题标题】:Chrome xpath expression for selenium does not find element硒的 Chrome xpath 表达式找不到元素
【发布时间】:2019-05-04 23:58:40
【问题描述】:

在网页中,我有以下元素:

....
<select class="widget-listbox form-control" size="6" multiple="multiple">
    <option data-value="sIPSCs%20from%20juvenile%20(P21-30)%20C57BL%2F6J%20male%20mice%20hippocampus%20CA1%20pyramidal%20cell%20(A1)" value="sIPSCs from juvenile (P21-30) C57BL/6J male mice hippocampus CA1 pyramidal cell (A1)" class="">sIPSCs&nbsp;from&nbsp;juvenile&nbsp;(P21-30)&nbsp;C57BL/6J&nbsp;male&nbsp;mice&nbsp;hippocampus&nbsp;CA1&nbsp;pyramidal&nbsp;cell&nbsp;(A1)</option>
    <option data-value="sIPSCs%20from%20juvenile%20(P21-30)%20C57BL%2F6J%20male%20mice%20hippocampus%20CA1%20pyramidal%20cell%20(A2)" value="sIPSCs from juvenile (P21-30) C57BL/6J male mice hippocampus CA1 pyramidal cell (A2)" class="">sIPSCs&nbsp;from&nbsp;juvenile&nbsp;(P21-30)&nbsp;C57BL/6J&nbsp;male&nbsp;mice&nbsp;hippocampus&nbsp;CA1&nbsp;pyramidal&nbsp;cell&nbsp;(A2)</option>
</select>
....

我正在尝试使用以下 xpath 表达式来选择第一个选项元素(用于 selenium 测试):

//option[contains(text(),"sIPSC")]
//option[text()="sIPSCs from juvenile (P21-30) C57BL/6J male mice hippocampus CA1 pyramidal cell (A1)"]
//option[@value="sIPSCs from juvenile (P21-30) C57BL/6J male mice hippocampus CA1 pyramidal cell (A1)"]
//select/option[contains(text(),"sIPSC")]
//select/option[text()="sIPSCs from juvenile (P21-30) C57BL/6J male mice hippocampus CA1 pyramidal cell (A1)"]
//select/option[@value="sIPSCs from juvenile (P21-30) C57BL/6J male mice hippocampus CA1 pyramidal cell (A1)"]

但没有显示任何结果(在 chrome 中,XPath Helper 版本 2.02;chrome 版本 70.0.3538.110)。我要查找的元素不在框架内。我还想选择 whole 字符串“sIPSCs from juvenile (P21-30) C57BL/6J male mice hippocampus CA1 pyramidal cell (A1)”,这可能是一个变量。随便一个字符串。我事先不知道这个字符串是什么样子的......

这次我做错了什么?上述任何表达都不应该起作用吗?

【问题讨论】:

  • 至少第一个应该可以工作。你换框架了吗?显示您的代码

标签: selenium google-chrome selenium-webdriver xpath


【解决方案1】:

看起来这两个选项都包含“sIPSC”

尝试使用 xpath 来识别元素的独特性,如下所示://option[contains(text(),"sIPSC")][contains(text(),"A1")]

如果您总是想要 first 选项: //select/option[contains(text(),"sIPSC")][1]

【讨论】:

  • 不抱歉,我想选择具有以下完整字符串的元素:“sIPSCs from juvenile (P21-30) C57BL/6J male mice hippocampus CA1 pyramidal cell (A1)”不只是部分.也许我事先不知道那个字符串...
【解决方案2】:

要选择第一个选项作为所需元素是 React 元素,您需要诱导 WebDriverWait 以使 元素可点击,您可以使用以下任一种(Pythonic)解决方案:

  • XPath 1

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[@class='widget-listbox form-control']/option[contains(@data-value,'(A1)')]"))).click()
    
  • XPath 2

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[@class='widget-listbox form-control']/option[contains(@value,'(A1)')]"))).click()
    
  • XPath 3

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[@class='widget-listbox form-control']/option[contains(.,'(A1)')]"))).click()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

【讨论】:

  • 不抱歉,我想选择具有以下完整字符串的元素:“sIPSCs from juvenile (P21-30) C57BL/6J male mice hippocampus CA1 pyramidal cell (A1)”不只是部分.也许我事先不知道那个字符串...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-17
  • 2020-08-17
  • 1970-01-01
相关资源
最近更新 更多