3

I am writing a bot using Python with Selenium module.When I open a webpage with my bot, since the webpage contains too many external sources than dom, it takes a lot to get all of the page loaded. I used the explicit and implicit waits to eliminate this problem since I just wanted a specific element to be loaded and not all of the webpage, it didn't work. The problem is If i run the following statement:

driver = webdriver.Firefox()
driver.get('somewebpage')
elm = WebDriverWait(driver, 5).until(ExpectedConditions.presence_of_element_located((By.ID, 'someelementID'))


elm.click()

It doesn't work since the Selenium waits for the driver.get() to fully retrieve the webpage and then, it proceeds further. Now I want to write a code that sets a timeout for driver.get(), Like:

driver.get('somewebpage').timeout(5)

Where the driver.get() stops loading the page after 5 secs and the program flow proceeds, whether the driver.get() fully loaded webpage or not.

By the way, I have searched about the feature that I said above, and came across there:

Selenium WebDriver go to page without waiting for page load

But the problem is that the answer in the above link does not say anything about the Python equivalent code.

How do I accomplish the future that I am searching for?

Community
  • 1
  • 1
The_Diver
  • 1,865
  • 4
  • 19
  • 19

3 Answers3

6

python equivalent code for the question mentioned in the current question (Selenium WebDriver go to page without waiting for page load):

from selenium import webdriver

profile = webdriver.FirefoxProfile()
profile.set_preference('webdriver.load.strategy', 'unstable')
driver = webdriver.Firefox(profile)

and:

driver.set_page_load_timeout(5)
Community
  • 1
  • 1
drets
  • 2,583
  • 2
  • 24
  • 38
3

There is a ton of questions on this, here is an example. Here is an example that waits until all jquery ajax calls have completed or a 5 second timeout.

from selenium.webdriver.support.ui import WebDriverWait

WebDriverWait(driver, 5).until(lambda s: s.execute_script("return jQuery.active == 0"))
Community
  • 1
  • 1
Pykler
  • 14,565
  • 9
  • 41
  • 50
3

It was a really tedious issue to solve. I just did the following and the problem got resolved:

driver= webdriver.Firefox()
driver.set_page_load_timeout(5)
driver.get('somewebpage')

It worked for me using Firefox driver (and Chrome driver as well).

Taher A. Ghaleb
  • 5,120
  • 5
  • 31
  • 44