1

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
2
  • can you share a link to that page? Commented Jan 8, 2022 at 17:53
  • @Prophet novasol.com Commented Jan 8, 2022 at 17:54

3 Answers 3

2

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:

driver.get("https://www.novasol.com/")

def retry_click(number_of_retries, wait_before_performing_click):
    while number_of_retries > 0:
        time.sleep(wait_before_performing_click)
        try:
            wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[onclick='CookieInformation.submitAllCategories();']"))).click()
            break
        except:
            pass
        number_of_retries = number_of_retries - 1


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()
    retry_click(20, 10)
    pass
Sign up to request clarification or add additional context in comments.

6 Comments

Used this, but this time it failed to accept cookies at the 17th try : TimeoutException: Message: timeout: Timed out receiving message from renderer: 298.955, maybe how could I add in except to refresh the link and try find the element again?
@Skittles: Updated, see above. if that still shows the issue, we would have to put a max_retry var and keep on retrying until we get succeed.
thank you, tried the updated code, but again failed on get(URL) 5th try, I could try to run it again because it doesn't fail every time, but would like to solve it that it works every time.
Need to create a function that will keep on retrying after a specified time and only exit once it's available to click.
could you please help with that how to write that? I tried to write in except : driver.quit( ), driver.get(link), wait.until.. but that didn't do anything.
|
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)

Comments

0

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...

2 Comments

for some reason sometimes it doesn't recognize the cookies pop up, I wanted to add in except: refresh the browser/link and try to find the element again.
OK, with refresh it should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.