尝试/例外在使用行增量的Xpath循环中未捕获NoSuchElementException

时间:2019-07-03 05:35:28

标签: python selenium nosuchelementexception

非常感谢。 我正在使用%格式来增加for循环内的行号,以在行上单击()来打开新标签页,抓取数据,关闭新标签页并单击下一行。该代码运行良好,直到它到达不是可点击元素的行为止。 try / except没有捕获异常。

错误消息:

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

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

1 个答案:

答案 0 :(得分:2)

异常来自try上方的行。尝试以下代码。

$stateProvider
  .state('app.home', {
      url: '/',
      controller: 'HomeCtrl as vm',
      template: homeView,
});
相关问题