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

seb*_*ebk 5 html selenium w3c web selenium-webdriver

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

<html><head></head><body></body></html>
Run Code Online (Sandbox Code Playgroud)

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

options.add_argument('--remote-debugging-port=9222')
options.add_argument("--no-sandbox")
...
Run Code Online (Sandbox Code Playgroud)

即使重新安装 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
Run Code Online (Sandbox Code Playgroud)

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

值得注意的是,页面有时会使用 selenium 正确加载,但通常不会。

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

<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html>
Run Code Online (Sandbox Code Playgroud)

Aad*_*Ura 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
Run Code Online (Sandbox Code Playgroud)

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