【发布时间】:2019-06-27 19:01:23
【问题描述】:
我正在尝试在 chrome 中使用 python 和 selenium 执行 Web 自动化。 事情是我试图找到一个没有 id 或类名的按钮。
xpath 是:
//*[@id="Form1"]/table[1]/tbody/tr/td/div/div[2]/table/tbody/tr[3]/td/span[1]
而html代码是
<span class="SectionMethod" onclick="window.location.href="explorer/explorer.aspx?root=user";" style="cursor:pointer;text-decoration:underline;color:CadetBlue;">Open</span>
这是一个名为 open 的按钮,但还有其他类似的按钮具有相同的文本和类,因此我无法通过文本定位。
这是我的代码:
from selenium import webdriver
driver = webdriver.Chrome(chrome_options=chromeOptions, desired_capabilities=chromeOptions.to_capabilities())
driver.get("..............")
driver.find_element_by_xpath('//*[@id="Form1"]/table[1]/tbody/tr/td/div/div[2]/table/tbody/tr[3]/td/span[1]')
这是我得到的错误:
NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"Form1"}
(Session info: chrome=75.0.3770.100)
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Windows NT 10.0.16299 x86_64).
【问题讨论】:
-
确保使用预期条件等到表单加载完毕。
标签: python selenium xpath css-selectors webdriverwait