Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Selenium WebDriver how to add timeout to get(url) function

I am running a simple piece of code that downloads a website through proxy, however sometimes the proxy can be slow and this can cause the WebDriver .get(url) request to block indefinitely.

Is there a simple piece of Python code for WebDriver that will set a timeout for this function? Through searching I've only found techniques that work for java.

driver.get(url)
like image 638
Aaron Hiniker Avatar asked Dec 13 '12 05:12

Aaron Hiniker


People also ask

How do I get current URL in Selenium WebDriver using Python?

You can easily get the current URL of a web page when performing Python automation testing — to do that, you just need to access the current_url method of the Selenium WebDriver object.

Why we use Get current URL command in Selenium?

Get Current URL Command In WebDriver, this method fetches the string representing the Current URL of the current web page. It accepts nothing as parameter and returns a String value.

How do I keep a browser session alive in Selenium?

We can keep a session alive for long Selenium scripts in automation. In Chrome browser, this can be achieved with the help of the ChromeOptions and Capabilities classes. Capabilities class can get the capabilities of the browser by using the method – getCapabilities.


1 Answers

For all the web cretents out there what I used to solve this problem was this. Selenium uses the socket library so I set a timeout on the socket module, this throws an error which we can use to send the escape key to the browser (which stops the page loading):

socket.setdefaulttimeout(2)
try:
     driver.get(pageLink)
except socket.timeout:
     #send ESCAPE key to browser
like image 123
Aaron Hiniker Avatar answered Oct 21 '22 15:10

Aaron Hiniker