bassboink
bassboink

Reputation: 88

Selenium with Python Random Timeout without error message

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!

Upvotes: 1

Views: 1174

Answers (2)

EnriqueBet
EnriqueBet

Reputation: 1473

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 

Upvotes: 2

SeleniumUser
SeleniumUser

Reputation: 4177

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

Upvotes: 0

Related Questions