My selenium webdriver been constantly crashing due to
TimeoutException: Message: timeout: Timed out receiving message from renderer: 298.972
The cookies pop up opens up but the script doesn't click on it, in like 20 driver.get(url)
, 19 times it will accept cookies but the 20th will fail to accept cookies, although the window has opened up, I tried to use the code below but still fails.
retries = 1
while retries <= 5:
try:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@class="coi-banner__accept"]'))) #wait until cookies clickable
element.click()
break
except TimeoutException:
driver.refresh()
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@class="coi-banner__accept"]'))) #wait until cookies clickable
element.click()
retries += 1
Try to use driver.execute_script() instead of element.click()
htmlElement = driver.find_element_by_xpath('//*[@class="coi-banner__accept"]')
driver.execute_script("arguments[0].click();", htmlElement)
I ran the below script more than 20 times and still it was able to click on the desired button every single time.
All I had to do was basically to change the locator to CSS from XPath:
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
wait = WebDriverWait(driver, 30)
driver.get("https://www.novasol.com/")
try:
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[onclick='CookieInformation.submitAllCategories();']"))).click()
print('Clicked it')
except:
print('Either element was not found, or Bot could not click on it.')
pass
Imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Updated:
try:
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[onclick='CookieInformation.submitAllCategories();']"))).click()
print('Clicked it')
except:
print('Either element was not found, or Bot could not click on it.')
driver.refresh()
time.sleep(5)
try:
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[onclick='CookieInformation.submitAllCategories();']"))).click()
except:
pass
pass
Generally,
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@class="coi-banner__accept"]'))) #wait until cookies clickable
element.click()
should work. From the code you shared I can't see why it works in 95% cases but fails in 5% case.
What I do see:
You are trying to find exactly the same condition in except
block that caused the TimeoutException
.
So, if somehow Selenium could not find element matching this locator //*[@class="coi-banner__accept"]
to be clickable and thrown an TimeoutException
waiting for the same condition in except
will give you the same result.... Also, I see no sense to put this in a loop of 5 attempts.
In case element was found, clicked and closed - no sense to try again.
And in case you could not do that in the first attempt - this will throw you TimeoutException
exception on the first attempt, you will never continue to the second attempt here...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.