硒driver.get (url)
等到整页加载。但是抓取页面会尝试加载一些无效的 JS 脚本。所以我的 Python 脚本等待它并且几分钟后不起作用。这个问题可能出现在网站的每个页面上。
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.cortinadecor.com/productos/17/estores-enrollables-screen/estores-screen-corti-3000')
# It try load: https://www.cetelem.es/eCommerceCalculadora/resources/js/eCalculadoraCetelemCombo.js
driver.find_element_by_name('ANCHO').send_keys("100")
如何限制等待时间、阻止文件的 AJAX 加载,还是其他方式?
我还测试了我的脚本webdriver.Chrome()
,但会使用 PhantomJS(),或者可能是 Firefox()。因此,如果某种方法使用浏览器设置的更改,那么它一定是通用的。
当 Selenium 默认加载页面/url 时,它遵循默认配置pageLoadStrategy
set to normal
。为了让 Selenium 不等待整页加载,我们可以配置pageLoadStrategy
. pageLoadStrategy
支持 3 个不同的值,如下所示:
-
normal
(整页加载)
-
eager
(交互的)
none
这是配置的代码块pageLoadStrategy
:
-
Firefox :
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities().FIREFOX
caps["pageLoadStrategy"] = "normal" # complete
#caps["pageLoadStrategy"] = "eager" # interactive
#caps["pageLoadStrategy"] = "none"
driver = webdriver.Firefox(desired_capabilities=caps, executable_path=r'C:\path\to\geckodriver.exe')
driver.get("http://google.com")
-
Chrome :
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities().CHROME
caps["pageLoadStrategy"] = "normal" # complete
#caps["pageLoadStrategy"] = "eager" # interactive
#caps["pageLoadStrategy"] = "none"
driver = webdriver.Chrome(desired_capabilities=caps, executable_path=r'C:\path\to\chromedriver.exe')
driver.get("http://google.com")
Note : pageLoadStrategy
values normal
, eager
and none
是一个要求WebDriver W3C 编辑草案 https://w3c.github.io/webdriver/#navigation but pageLoadStrategy
价值为eager
仍然是一个WIP(进行中的工作) within Chrome驱动程序执行。您可以在中找到详细的讨论Python 中 Chromedriver Selenium 的“Eager”页面加载策略解决方法 https://stackoverflow.com/questions/51087832/eager-page-load-strategy-workaround-for-chromedriver-selenium-in-python
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)