用于选择相对于当前节点的元素的 Xpath 表达式



我在Python中使用Selenium Webdriver。我想用type="text"type="password"type="button"遍历并计算可见输入元素的数量。我想通过首先提取输入标签,然后遍历它们来做到这一点。

这是我尝试过的 -

Text_Xpath2 = "./[@type='text' or @type='email' or @type='username']"
Pass_Xpath2 = "./[@type='password']"
Button_Xpath2 = "./[@type='button' or @type='submit']"
or 
Text_Xpath2 = "[@type='text' or @type='email' or @type='username']"
Pass_Xpath2 = "[@type='password']"
Button_Xpath2 = "[@type='button' or @type='submit']"

inputs = browser.find_elements_by_xpath('//input')
for input in inputs:
if input.is_displayed():
text_fields = input.find_element_by_xpath(Text_Xpath2)
pass_fields = input.find_element_by_xpath(Pass_Xpath2)
buttons = input.find_element_by_xpath(Button_Xpath2)

我总是收到这个错误 -

字符串 './[@type='text' 或 @type='email' 或 @type='username']' 是 不是有效的 XPath 表达式。

有人可以指出错误是什么吗?

您缺少标签,例如<div>

".//div[@type='text' or @type='email' or @type='username']"

或任何标签

".//*[@type='text' or @type='email' or @type='username']"

在您不需要它的情况下,.inxpath用于定位当前元素的后代元素,而不是再次重新定位相同的元素。尝试

inputs = browser.find_elements_by_xpath('//input[@type="text" or @type="email" or @type="username"]')

最新更新