简体   繁体   English

在 Try 块 WebDriverWait 中出现错误 - Python Selenium

[英]Getting error inside Try block WebDriverWait - Python Selenium

I'm trying a Try/Exception block that uses Selenium to make click in a web button.我正在尝试使用 Selenium 来点击 web 按钮的 Try/Exception 块。 But even if I use a Try/Exception block, the code breaks inside the Try block.但即使我使用 Try/Exception 块,代码也会在 Try 块内中断。 Part of the code is the following:部分代码如下:


...
###### click left bar option
select_element(driver, By.CSS_SELECTOR, "a[id='mnu_Matrícula']")

###### click suboption
select_element(driver, By.XPATH, "//*[@id='2020']")
time.sleep(20)
            
try:
    # click new creation
   WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/div[1]/div[1]/div/div[1]/div[1]/app-panel/div/div[2]/div/button"))).click()
    # select_element(driver, By.XPATH, "/html/body/div[1]/div[1]/div[1]/div/div[1]/div[1]/app-panel/div/div[2]/div/button")
    time.sleep(10)
except:
    status = "ERROR"

...

The error I'm getting mentions:我提到的错误:

Cell In[23], line 121
   119 try:
   120     # click new creation
--> 121     WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/div[1]/div[1]/div/div[1]/div[1]/app-panel/div/div[2]/div/button"))).click()
   122     #select_element(driver, By.XPATH, "/html/body/div[1]/div[1]/div[1]/div/div[1]/div[1]/app-panel/div/div[2]/div/button")

   raise TimeoutException(message, screen, stacktrace)

I also have to mention that I iterate this code many times, and this error happens once each N times, but I don't get why.我还必须提到我多次重复此代码,并且此错误每 N 次发生一次,但我不明白为什么。

Thanks for your help.谢谢你的帮助。

I'm pretty sure you have to specify which Exception needs to occur for the except block to be utilized, as in:我很确定您必须指定要使用的 except 块需要发生哪个异常,如:

try:
    # click new creation
   WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/div[1]/div[1]/div/div[1]/div[1]/app-panel/div/div[2]/div/button"))).click()
    # select_element(driver, By.XPATH, "/html/body/div[1]/div[1]/div[1]/div/div[1]/div[1]/app-panel/div/div[2]/div/button")
    time.sleep(10)
except TimeoutException:
    status = "ERROR"

Don't forget to import the Exception as well.不要忘记也导入异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM