Python 请尝试/除了在使用行增量的Xpath循环中不捕获NoTouchElementException之外
非常感谢。 我正在使用%格式增加for循环中的行号,以便在行上单击()以打开新选项卡、刮取数据、关闭新选项卡并单击下一行。代码运行良好,直到它遇到一行不是可单击元素。try/except未捕获异常 错误消息:Python 请尝试/除了在使用行增量的Xpath循环中不捕获NoTouchElementException之外,python,selenium,nosuchelementexception,Python,Selenium,Nosuchelementexception,非常感谢。 我正在使用%格式增加for循环中的行号,以便在行上单击()以打开新选项卡、刮取数据、关闭新选项卡并单击下一行。代码运行良好,直到它遇到一行不是可单击元素。try/except未捕获异常 错误消息: selenium.common.exceptions.NoSuchEleme ntException: Message: Unable to locate element: .//table[1]/tbody/tr[3]/td[3] 我在这里有点迷路了。我不太确定如何处理这个问题,因为循
selenium.common.exceptions.NoSuchEleme ntException: Message: Unable to locate element: .//table[1]/tbody/tr[3]/td[3]
我在这里有点迷路了。我不太确定如何处理这个问题,因为循环在整个范围内运行
from selenium.common.exceptions import NoSuchElementException
row_start = 2
x = len(driver.find_elements_by_xpath('.//table[1]/tbody/tr'))
for c in range(row_start, x + 1):
table_row = driver.find_element_by_xpath('.//table[1]/tbody/tr[%d]/td[3]' % c)
try:
table_row.click()
except NoSuchElementException:
continue
table_row.click()
其目的是当某一行不可单击时,跳过该行并继续下一行。例外情况来自try上方的行。请尝试使用以下代码
from selenium.common.exceptions import NoSuchElementException
row_start = 2 x = len(driver.find_elements_by_xpath('.//table[1]/tbody/tr'))
for c in range(row_start, x + 1):
try:
table_row = driver.find_element_by_xpath('.//table[1]/tbody/tr[%d]/td[3]' % c)
table_row.click()
except NoSuchElementException:
continue
非常感谢。为了解决这个问题,我已经好几天没睡了。再次感谢你。