1

I am following the example here (under the Python tab): https://www.selenium.dev/documentation/en/

Code here:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support.expected_conditions import presence_of_element_located

    #This example requires Selenium WebDriver 3.13 or newer
    with webdriver.Firefox() as driver:
        wait = WebDriverWait(driver, 10)
        driver.get("https://google.com/ncr")
        driver.find_element_by_name("q").send_keys("cheese" + Keys.RETURN)
        first_result = wait.until(presence_of_element_located((By.CSS_SELECTOR, "h3>div")))
        print(first_result.get_attribute("textContent"))

I've ran this code and got it to work, displaying the first result "Show More". However, other times when I run this code this doesn't work, and gives a random timeout error without a message:

    Traceback (most recent call last):
    File "c:\Users\User\PyCharm_Projects\Project\sample_test.py", line 12, in <module>
        first_result = wait.until(presence_of_element_located((By.CSS_SELECTOR, "h3>div")))
    File "C:\Program Files\Python38\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
        raise TimeoutException(message, screen, stacktrace)
    selenium.common.exceptions.TimeoutException: Message:

My question is: what is causing the timeout error if it doesn't happen every time? I've tried locating the elements with other methods (XPATH, link text). I've looked at the other following examples, but nothing that they post seemed to fix this problem:

Tried, didn't work - Selenium random timeout exceptions without any message

Non-applicable solutions - Instagram search bar with selenium - Selenium Timeoutexception Error - Random TimeoutException even after using ui.WebDriverWait() chrome selenium python

I am on Python 3.8, Firefox 68.6.0, and here is the relevant packages from 'pip freeze' - beautifulsoup4==4.8.2 - requests==2.22.0 - selenium==3.141.0 - urllib3==1.25.8 - webencodings==0.5.1

Thank you!

4
  • 1
    Hello, I had this very same problem with a project in the past. The only workaround that I found was to implement try/except logic and use time.sleep(x) before retrying. Commented Mar 20, 2020 at 18:01
  • Hi! Just to be clear: was the issue just related to slow server times? If so, would doing something like wait = WebDriverWait(driver, 1000) assure that everything loaded. I guess I'm just not sure what the root cause is in my case. Commented Mar 20, 2020 at 18:09
  • 1
    In my case, the issue was that the website refreshed my web element, making it unexistent by a short period of time. Because the web driver already assigned an id to this element, it couldn't found it. So this try/ exception logic worked for me pretty well Commented Mar 20, 2020 at 18:17
  • This seems like this works well for me! It's annoying to have try/except around every element I fetch, but it seems to work! (Maybe the issue was also I hadn't included a finally: driver.quit() block at the end of everything). You can make an answer summarizing this and I can make it the accepted answer! Thanks! Commented Mar 20, 2020 at 19:35

2 Answers 2

2

I have made some customized actions for this cases, for instance:

def findXpath(xpath,driver):
    actionDone = False
    count = 0
    while not actionDone:
        if count == 3:
            raise Exception("Cannot found element %s after retrying 3 times.\n"%xpath)
            break
        try:
            element = WebDriverWait(driver, waitTime).until(
                    EC.presence_of_element_located((By.XPATH, xpath)))
            actionDone = True
        except:
            count += 1
    sleep(random.randint(1,5)*0.1)
    return element 
Sign up to request clarification or add additional context in comments.

Comments

0

Please refer below solution, I have executed this on a chrome and firefox browser couple of times and its working fine.TimeoutException thrown when a command does not complete in enough time.

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

#utilise chrome driver to open specified webpage
driver = webdriver.Chrome(executable_path=r"chromedriver.exe")
driver.maximize_window()
driver.get("https://google.com/ncr")
WebDriverWait(driver, 20).until(
EC.visibility_of_element_located((By.NAME,"q"))).send_keys("cheese" + Keys.RETURN)


first_result=WebDriverWait(driver, 20).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, "h3>div")))

print(first_result.get_attribute("textContent"))

Output

Show more

Comments

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.