我正在嘗試抓取一個 JS 網(wǎng)站。我使用了 Selenium,在出現(xiàn)此錯誤之前一切正常。該網(wǎng)站有一張桌子,我正在處理它。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。我嘗試使用這樣的“循環(huán)傳遞”來解決問題: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貢獻(xiàn)2080條經(jīng)驗 獲得超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。有用

函數(shù)式編程
TA貢獻(xiàn)1807條經(jīng)驗 獲得超9個贊
你試過這樣的事情嗎?for
(你可以很容易地用循環(huán)啟動它)
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
提交
取消