首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Selenium + Python:无法用Try & WebDriverWait捕获WebDriverWait

Selenium + Python:无法用Try & WebDriverWait捕获WebDriverWait
EN

Stack Overflow用户
提问于 2022-03-21 12:10:14
回答 1查看 1.4K关注 0票数 0

:PyCharm 2021.3.2 (CE);OS: fedora35VM

我正在编写一个python selenium脚本来从一个网站中抓取数据。我想浏览一个网站,找到一个元素并打印出来。当元素存在时,我能够做到这一点。我的问题是,有时元素不存在&我得到了一个异常。如果在使用此代码时元素不存在:

代码语言:javascript
运行
复制
 RemainDeductible = (WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "b8-b36-Input_RemainAmtYr1"))).get_attribute("value"))

脚本产生以下异常:

代码语言:javascript
运行
复制
 Traceback (most recent call last):
  
 File "...PythonSeleniumScript.py", line 152, in <module>
    RemainDue = (WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "b8-b36-Input_RemainAmtYr1"))).get_attribute("value"))

 File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/support/wait.py", line 89, in until
    raise TimeoutException(message, screen, stacktrace)

 selenium.common.exceptions.TimeoutException: Message: 
Stacktrace:
WebDriverError@chrome://remote/content/shared/webdriver/Errors.jsm:183:5
NoSuchElementError@chrome://remote/content/shared/webdriver/Errors.jsm:395:5
element.find/</<@chrome://remote/content/marionette/element.js:300:16

如果发生超时值异常,我想捕获异常&将"Remain“变量设置为字符串”空白“。如果在代码中使用Try,仍然会得到以下超时异常,如下所示:

代码语言:javascript
运行
复制
try:
    RemainDue = (WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.ID, "b8-b36-Input_RemainAmtYr1"))).get_attribute("value"))
except TimeoutException:
    RemainDue = "BLANK"

我还是有超时值异常。我以为我的代码会抓住例外。为什么它没有抓住它?

代码语言:javascript
运行
复制
 Traceback (most recent call last):
  
 File "...PythonSeleniumScript.py", line 155, in <module>
    RemainDue = (WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.ID, "b8-b36-Input_RemainAmtYr1"))).get_attribute("value"))

  File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/support/wait.py", line 89, in until

    raise TimeoutException(message, screen, stacktrace)

 selenium.common.exceptions.TimeoutException: Message: 
Stacktrace:
WebDriverError@chrome://remote/content/shared/webdriver/Errors.jsm:183:5
NoSuchElementError@chrome://remote/content/shared/webdriver/Errors.jsm:395:5
element.find/</<@chrome://remote/content/marionette/element.js:300:16

我试图找到的元素的相关HTML是:

代码语言:javascript
运行
复制
<input id="b8-b36-Input_RemainAmtYr1"
class="form-control OSFillParent" data-
input="" disabled="" type="text"
style="margin-top: 5px;" value="$10.50">
event

当然,当元素存在时,代码也能工作。我的问题是,当元素为而不是时,代码会与超时异常一起崩溃。当超时异常发生时,我如何捕获超时异常&将“仍然到期”变量设置为字符串“空白”?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-21 12:24:58

我想在这里提出几点建议:

  1. 而不是

代码语言:javascript
运行
复制
except TimeoutException:

试着使用

代码语言:javascript
运行
复制
except:

我知道最好捕获特定的异常类型,而不是任何异常,但我仍然建议在这里这样做。

2)我建议使用visibility_of_element_located而不是presence_of_element_located,因为presence_of_element_located只会等待页面上出现这样的元素,而不是完全呈现。而visibility_of_element_located将等待更成熟的元素状态,当它是可见的。

所以而不是

代码语言:javascript
运行
复制
try:
    RemainDue = (WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.ID, "b8-b36-Input_RemainAmtYr1"))).get_attribute("value"))
except TimeoutException:
    RemainDue = "BLANK"

我建议你用

代码语言:javascript
运行
复制
try:
    RemainDue = (WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.ID, "b8-b36-Input_RemainAmtYr1"))).get_attribute("value"))
except:
    RemainDue = "BLANK"

还要确保b8-b36-Input_RemainAmtYr1b8-b36-Input_RemainAmtYr1值是固定的,而不是更改的。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71557425

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档