Setting page load timeout in Selenium Python binding

房东的猫 提交于 2019-12-22 00:40:44

问题


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?


回答1:


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)



回答2:


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"))



回答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).



来源:https://stackoverflow.com/questions/32276654/setting-page-load-timeout-in-selenium-python-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!