尝试/除非在使用行增量的XPath循环中未捕获NosuchelementException



非常感谢。我正在使用%格式来在循环中递增行号,以单击"行",以打开新选项卡,刮擦数据,关闭新选项卡,然后单击下一行。该代码运行良好,直到击中不是可单击的元素的行。尝试/除外,没有捕获异常。

错误:

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()

目的是当一行无法单击该行并继续到下一行时。

例外是从尝试上方的行。尝试以下代码。

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

最新更新