問題描述
Selenium driver.get (url)
等待整個頁面加載.但是一個抓取頁面會嘗試加載一些死掉的 JS 腳本.所以我的 Python 腳本等待它并且不能工作幾分鐘.這個問題可能出現在網站的每個頁面上.
Selenium driver.get (url)
wait till full page load. But a scraping page try to load some dead JS script. So my Python script wait for it and doesn't works few minutes. This problem can be on every pages of a site.
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加載,或者其他方式?
How to limit the time wait, block AJAX load of a file, or is other way?
我還在 webdriver.Chrome()
中測試我的腳本,但會使用 PhantomJS(),或者可能是 Firefox().因此,如果某些方法使用了瀏覽器設置的更改,那么它必須是通用的.
Also I test my script in webdriver.Chrome()
, but will use PhantomJS(), or probably Firefox(). So, if some method uses a change in browser settings, then it must be universal.
推薦答案
當 Selenium 默認加載頁面/url 時,它遵循默認配置,將 pageLoadStrategy
設置為 normal
.為了使 Selenium 不等待整個頁面加載,我們可以配置 pageLoadStrategy
.pageLoadStrategy
支持以下 3 種不同的值:
When Selenium loads a page/url by default it follows a default configuration with pageLoadStrategy
set to normal
. To make Selenium not to wait for full page load we can configure the pageLoadStrategy
. pageLoadStrategy
supports 3 different values as follows:
正常
(全頁加載)渴望
(交互式)無
這是配置pageLoadStrategy
的代碼塊:
Here is the code block to configure the pageLoadStrategy
:
火狐:
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 ogeckodriver.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 ochromedriver.exe')
driver.get("http://google.com")
注意 : pageLoadStrategy
值 normal
, eager
和 none
是 WebDriver W3C Editor's Draft 但 pageLoadStrategy
值作為 eager
仍然是一個WIP(Work InChromeDriver 實施中的進展).您可以在 渴望"頁面加載中找到詳細討論Python 中 Chromedriver Selenium 的策略解決方法
Note :
pageLoadStrategy
valuesnormal
,eager
andnone
is a requirement as per WebDriver W3C Editor's Draft butpageLoadStrategy
value aseager
is still a WIP (Work In Progress) within ChromeDriver implementation. You can find a detailed discussion in "Eager" Page Load Strategy workaround for Chromedriver Selenium in Python
這篇關于如何讓 Selenium 不等到整個頁面加載,它的腳本很慢?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!