Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
165 views
in Technique[技术] by (71.8m points)

How would I make my code reload instead of crashing after an error in python selenium chromedriver?

I have it as wait until clickable so I can keep reloading on my own but i want it to just do it on it's own because constantly reloading isn't fun and is a waste of time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


driver = webdriver.Chrome()
driver.get('link.com')

wait = WebDriverWait(driver, 1000)
AddToKart = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="content"]/div/div/div[1]/div[3]/div[1]/section[2]/section/div[15]/div/div[2]/button'))).click()```




question from:https://stackoverflow.com/questions/65866807/how-would-i-make-my-code-reload-instead-of-crashing-after-an-error-in-python-sel

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
wait = WebDriverWait(driver, 5)
count = 1
while(count==1):
    count=0
    try:
       wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="content"]/div/div/div[1]/div[3]/div[1]/section[2]/section/div[15]/div/div[2]/button'))).click()    

    except Exception as e:
        print(e)
        count=1
        try:
            driver.refresh()
        except Exception as e:
            print(e)

Just wrapp it with while and try/except


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...