硒测试无法定位元素

内登范

我正在使用 selenium 在我们的网站上运行一些测试。在登录页面,我想定位登录按钮并单击它。

页面的源代码如下所示:在此处输入图片说明

我试图class=OTSigninButton通过使用其 xpath来定位具有 的第二个按钮

所以这是python代码。

time.sleep(5)
element = driver.find_element_by_xpath('//*[@id="root"]/div/div[1]/div/svg[2]/path')
element.click()

但是当我运行代码时,出现以下错误:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="root"]/div/div[1]/div/svg[2]/path"}
  (Session info: chrome=91.0.4472.106)

这是奇怪的部分。登录页面有 2 个按钮,它们的 xpath 与其列表类似。

第一个按钮是:

//*[@id="root"]/div/div[1]/div/svg[1]/path

第二个按钮(我想要定位的那个)是

//*[@id="root"]/div/div[1]/div/svg[1]/path

由于他们都有id=root,如果我find_element_by_id和目标根,我可以点击第一个按钮。

这是我苦苦挣扎的地方,如何使用 xpath 定位第二个按钮?

非常感谢您的时间和帮助家伙。

编辑:完全错误

Traceback (most recent call last):
  File "test.py", line 17, in <module>
    element = driver.find_element_by_xpath("//*[local-name()='svg' and contains(@class, 'OTSigninButton')]/title")
  File "/Users/<user>/opt/anaconda3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "/Users/<user>/opt/anaconda3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "/Users/<user>/opt/anaconda3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/Users/<user>/opt/anaconda3/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[local-name()='svg' and contains(@class, 'OTSigninButton')]/title"}
  (Session info: chrome=91.0.4472.106)

游轮潘迪
class=OTSigninButton

是 的一个属性SVG这些是特殊标签,您不能只在控制台中写入它们的标签名称并找到它们。

试试这个:

//*[local-name()='svg' and contains(@class, 'OTSigninButton')]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章