'Selenium(Python) wait 10 sec for searching id on the page

I have some example of the code:

def wait_for_id(self, id):
    self.wait.until(EC.element_to_be_clickable((By.ID, id)))
    return self.browser.find_element_by_id(id)

I need to wait only 10 sec for finding this element on the page. If it will be more than 10 sec, so need to fail and continue next row.

self.wait_for_id('quit').click()

So by default, there is a time for waiting is 30 minutes(around this time)... then it failed as I expected, but I want to put for example only 2 minutes maximum on waiting for failure.



Solution 1:[1]

The WebDriverWait() constructor takes a WebDriver instance and timeout in seconds.

So essentially before the following code block:

def wait_for_id(self, id): 
    self.wait.until(EC.element_to_be_clickable((By.ID, id))) 
    return self.browser.find_element_by_id(id)
    

somewhere in your code you have defined wait as:

self.wait = WebDriverWait(self.driver, 1800)

Hence, time for waiting is 30 minutes.

To reduce the waiting time to 2 minutes you need to change it as:

self.wait = WebDriverWait(self.driver, 120)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 undetected Selenium