Selenium(Chrome)打开空白页面并返回<html><head></head><body></body></html>

问题描述 投票:0回答:2

如果我使用 Selenium 打开网站(无头或无头),则会打开一个空白页面,输出为:

<html><head></head><body></body></html>

如果我在浏览器中手动打开页面,网站加载正常。我用 time.sleep(10) 尝试过,删除了随机数,然后尝试了不同的参数,例如:

options.add_argument('--remote-debugging-port=9222')
options.add_argument("--no-sandbox")
...

即使重新安装 chromedriver 也没有帮助。

目前还没有成功。

这是我的代码:

url = "https://www.arket.com/de_de/men/knitwear/product.alpaca-blend-jumper-grey.0937502001.html"
options = Options()
ua = UserAgent()
userAgent = ua.random
options.add_argument(f'user-agent={userAgent}')
#options.add_argument("headless")
driver = webdriver.Chrome('/path/to/chromedriver', options=options)
time.sleep(5)
driver.get(url)
time.sleep(5)

soup = BeautifulSoup(driver.page_source, 'html.parser')
time.sleep(0.5)
driver.quit()

soup

有谁知道如何解决这个问题吗?

页面有时会使用 selenium 正确加载,这可能会很值得注意,但通常情况下不会。

昨天代码的输出有所不同:

<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html>
html selenium selenium-webdriver web w3c
2个回答
0
投票

我也面临着类似的问题。我的代码如下所示:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.by import By



chrome_options                    = webdriver.ChromeOptions()
chrome_options.headless           = True
capabilities = DesiredCapabilities.CHROME

driver = webdriver.Chrome(desired_capabilities = capabilities, 
                            chrome_options          = chrome_options)

driver.get('https://stackoverflow.com')
driver.page_source

问题既不在于网站,也不在于评论中提到的用户代理。我将 selenium 从 4.7.0 降级到 4.2.0,现在可以使用了。最新版本的 selenium 有一些问题。


0
投票

添加用户代理 chrome_options.add_argument(„user-agent=derry“) 对我有用

© www.soinside.com 2019 - 2024. All rights reserved.