【发布时间】: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