我正在尝试抓取一个 JS 网站。我使用了 Selenium,在出现此错误之前一切正常。该网站有一张桌子,我正在处理它。selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/table/tbody/tr[2]/td[2]/table/tbody/tr[26]/td[4]"}
(Session info: chrome=84.0.4147.125)在 XPath 第 25 和第 27 次迭代中有 4.td/html/body/table/tbody/tr[2]/td[2]/table/tbody/tr[26]/td[4]但是 26th 没有第 4 个 td。我尝试使用这样的“循环传递”来解决问题:if(bool(driver.find_element_by_xpath("/html/body/table/tbody/tr[2]/td[2]/table/tbody/tr[26]/td[***+str(j)+***] ").is_enabled)):pass但是,一旦 selenium 看到路径,它就会在不检查的情况下给出错误。我知道“is_enable”方法毫无用处。问题是:如何在没有 Selenium 中断的情况下通过迭代?
2 回答

犯罪嫌疑人X
TA贡献2080条经验 获得超4个赞
try:
driver.find_element_by_xpath("./html/body/table/tbody/tr[2]/td["+str(j)+"]/table/tbody/tr["+str(i)+"]/td[4]")
except NoSuchElementException:
continue
如果 XPath 中没有元素,则返回 null。有用

函数式编程
TA贡献1807条经验 获得超9个赞
你试过这样的事情吗?for
(你可以很容易地用循环启动它)
if driver.find_element_by_xpath("/html/body/table/tbody/tr[2]/td[2]/table/tbody/tr[26]/td[1(Can't boosted with for loop): print("Found object") else: print("Can't find object")
添加回答
举报
0/150
提交
取消