如何检查 for 循环中的下一次迭代?

我正在尝试抓取一个 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 中断的情况下通过迭代?


森林海
浏览 87回答 2
2回答

犯罪嫌疑人X

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。有用

函数式编程

你试过这样的事情吗?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")
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python