【问题标题】:Couldn't find an element with Selenium 2 by xpathxpath 找不到带有 Selenium 2 的元素
【发布时间】:2014-10-24 10:40:40
【问题描述】:

我需要在 HTML 中找到这个元素

<input id="hidder_form:j_idt286:parnterCategoryId" name="hidder_form:j_idt286:parnterCategoryId" type="text" value="" class="ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all" role="textbox" aria-disabled="false" aria-readonly="false" aria-multiline="false">

我就是这样做的

使用 XPATH,由 Chrome 控制台生成:

WebDriverWait(driver, 10).until_not(EC.presence_of_element_located((By.XPATH, '//*[@id="hidder_form:j_idt286:parnterCategoryId"])))

使用 XPATH,它正在寻找一种正则表达式:

WebDriverWait(driver, 10).until_not(EC.presence_of_element_located((By.XPATH, '//*[starts-with(@id, "hidder_form") and ends-with(@id, "parnterCategoryId")]')))

最后使用 XPATH,它通过页面完全到达我的元素

'/html/body/section[@id = "hidder"]/form[@id="hidder_form"]/div[@id="forms"]/div[@class="ui-outputpanel ui-widget"]/section[@id="category_edit_form"]/div[@class="column-container"]/div[@class="left column50per"]/fieldset[@class = "properties"]/div/input[ends-with(@id, "parnterCategoryId")]'

它们都不起作用。全部给出一个例外

selenium.common.exceptions.InvalidSelectorException: Message: u'The given selector Unable to locate an element with the xpath expression because of the following error:\nSyntaxError: The expression is not a legal expression.

【问题讨论】:

  • 元素的id是动态的吗?
  • 是的,我认为它在 hidder_form 和 parnterCategoryId id="hidder_form:j_idt286:parnterCategoryId" 之间有动态部分
  • 你不能通过 id 而不是 xpath 找到元素吗?
  • @thepolina 我也很怀疑。我很困惑为什么使用 'starts-with()` 和 end-with() 的 xpath 不起作用。如果将 xpath 的开头从 //* 更改为 //input,会发生什么?
  • @MarkRowlands 如果我将其更改为输入它会给出相同的异常

标签: python html python-2.7 selenium xpath


【解决方案1】:

ends-with 是 XPath 2.0 函数,您的浏览器可能使用 XPath 1.0 处理器(Selenium 使用浏览器处理器)。

XPath 支持:Comparison of layout engines (XML)

【讨论】:

  • 我认为你是对的。我使用 FireFox,根据这个 documentation 它无法处理 ends-with()
猜你喜欢
  • 2018-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-20
  • 2021-03-20
  • 2014-08-19
  • 1970-01-01
相关资源
最近更新 更多