在Selenium Python绑定中设置页面加载超时 [英] Setting page load timeout in Selenium Python binding

查看:351
本文介绍了在Selenium Python绑定中设置页面加载超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有Selenium模块的Python编写机器人。当我使用机器人打开一个网页时,由于该网页包含的外部源数量超过了dom,所以需要很多东西来加载所有的页面。我使用显式和隐式的等待来消除这个问题,因为我只想要一个特定的元素被加载,而不是所有的网页,它没有工作。问题是如果我运行以下语句:

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


elm.click()

它不起作用,因为Selenium等待driver.get()完全检索网页,现在我想写一个为driver.get()设置超时的代码,如:

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

5秒后加载页面,程序流程继续进行,驱动程序.get()是否完全加载网页。



顺便说一下,我已经搜索了该功能我上面说过,并且遇到了:



Selenium WebDriver转到页面,而不用等待页面加载



但问题是上面的链接中的答案没有说什么关于Python



如何完成我正在寻找的未来?

解决方案

div>

python 当前问题中提到的问题的等效代码( Selenium WebDriver转到页面,而不用等待页面加载):

 从selenium import webdriver 

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

和:

  driver.set_page_load_timeout(5)


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?

解决方案

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)

这篇关于在Selenium Python绑定中设置页面加载超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆