python - 处理几页硒脚本后出现NoSuchElementException错误
问题描述
我想知道是否有任何方法可以保护脚本在由于 NoSuchElementException 错误而退出之前不被破坏。我的脚本适用于网站上的前几页,并且能够找到元素,但在前几页之后,由于没有此类元素错误,它退出了。我想知道如何防止程序退出并一直到页面末尾。抱歉,我是堆栈溢出的新手,所以如果我问得正确,我不会。谢谢!
line 39, in <module>
main3 = main.find_element_by_xpath('//*[@id="main-content-column"]/div/div[7]/div/div[2]/div/div/ng-transclude/table/tbody')
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="main-content-column"]/div/div[7]/div/div[2]/div/div/ng-transclude/table/tbody"}
解决方案
并非所有元素都具有相同的 Xpath 或 Css。尝试查看它何时失败 - 也许该元素有另一个表达式。
另外,尝试使用这个:
WebDriverWait wait = new WebDriverWait(browser, secondsToWait);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id(elementID)));
有许多 ExpectedConditions,如 elementToBeClickable、elementToBeVissible 等。
推荐阅读
- javascript - Vue问题显示来自rest api的json响应
- numpy - 在 numpy 中实现反向传播
- r - 如何从 R 中的矩阵(获取顺序)创建行号向量?
- r - 粘贴并获取相应变量的值
- java - PGP 库不会加密 base64 字符串
- c# - 通过 Lamda 表达式使用 Select 和 Where 子句连接多个表
- java - 进度条 Java 退出
- php - PHP-DI:如何使用自定义构造函数参数值创建接口实现?
- sql - Presto - 如何在一个查询中的所有列之间执行相关性
- python - 如何使用 Django-Scheduler 将 onClick 操作添加到日历事件?