Selenium 可与 Chrome 配合使用,但不能与无头 Chrome 配合使用

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

我使用 Selenium 和最初的 PhantomJS 开发了几个 Python 脚本。在走向自动下载的过程中,我切换到了(有头)Firefox(有效),然后是带有无头选项的 Chrome,这样我就不会在我面前打开浏览器了。

我的第一个脚本访问一个页面和几个 HTML 元素,与无头 Chrome 完美配合。

但是,第二个仅适用于带头的Chrome。如果我添加“无头”选项,它就不再起作用了。当我尝试以无头模式打印 HTML 以查看为什么它找不到我正在寻找的 HTML 元素时,我所拥有的只是:

<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html>

使用带头的 Chrome,我打印了完整的 HTML。 这就是我开始我的无头 Chrome 的方式:

options = webdriver.ChromeOptions()
options.add_argument("--ignore-certificate-errors") 
options.add_argument("headless") 
driver = webdriver.Chrome(chrome_options=options)

再次注意,这适用于我的另一个脚本。这里唯一的区别是我需要登录才能访问该页面,但即便如此,为什么它可以与头部一起工作?我的脚本是通过填写表单自动登录的。

Python:3.6.1,Chrome:60.0.3112.78(64 位),Selenium:3.4.3

有什么想法吗? 谢谢。

** 编辑:这是代码的开头**

url = 'https://10.11.227.21/tmui/'
driver.get(url + "login.jsp")

html_source = driver.page_source
print(html_source)

blocStatus = WebDriverWait(driver, TIMEOUT).until(EC.presence_of_element_located((By.ID, "username")))
inputElement = driver.find_element_by_id("username")
inputElement.send_keys('actualLogin')
inputElement = driver.find_element_by_id("passwd")
inputElement.send_keys('actualPassword')
inputElement.submit()
python google-chrome selenium headless
7个回答
11
投票

我和你有同样的经历,并通过使用xvfb和pyvirtualdisplay解决了它。

我使用 chromedrive=v2.3.1、chrome-browser=v60 和 Selenium=3.4.3

在 Headless chrome 中,某些脚本似乎无法按预期工作。

请参阅vpassapera的评论https://gist.github.com/addyosmani/5336747

像下面这样尝试一下怎么样,

from pyvirtualdisplay import Display

display = Display(visible=0, size=(800, 600))
display.start()

# Do Not use headless chrome option
# options.add_argument('headless')

url = 'https://10.11.227.21/tmui/'
driver.get(url + "login.jsp")

html_source = driver.page_source
print(html_source)

blocStatus = WebDriverWait(driver,    TIMEOUT).until(EC.presence_of_element_located((By.ID, "username")))
inputElement = driver.find_element_by_id("username")
inputElement.send_keys('actualLogin')
inputElement = driver.find_element_by_id("passwd")
inputElement.send_keys('actualPassword')
inputElement.submit()

display.stop()

xvfb 需要使用“pyvirtualdisplay”

$ sudo apt-get install -y xvfb 

7
投票

Headless Chrome 不支持不安全的证书,因此,具有不安全证书的网站无法打开。您需要添加如下功能:

from selenium import webdriver
from selenium.webdriver import DesiredCapabilities
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--headless")

capabilities = DesiredCapabilities.CHROME.copy()
capabilities['acceptSslCerts'] = True 
capabilities['acceptInsecureCerts'] = True

driver = webdriver.Chrome(chrome_options = chrome_options,executable_path='your path',desired_capabilities=capabilities)
driver.get("yourWebsite")

这样就可以完成工作了。


3
投票

我遇到了同样的问题,并在 conftest.py 中设置窗口大小解决了它。 我的代码片段:

@pytest.fixture
def chrome_options(chrome_options, pytestconfig):
    if pytestconfig.getoption('headless'):
        chrome_options.add_argument('--headless')
        chrome_options.add_argument("window-size=1920,1080")
    else:
        chrome_options.add_argument("start-maximized");
    return chrome_options

0
投票

无头 chrome 在同一台机器上可能比有头更快,尝试在定位密码元素之前添加一些等待。


0
投票

对于某些人来说,如果删除以下内容,就可以了。

driver.fullscreen_window()

0
投票

对于我的情况,无头不起作用,因为我在代理后面。显然,Chrome 可以使用系统代理,但 headless 不使用系统代理。

我只需要提供代理,然后 headless(以及 Chrome)就可以工作了。

options.add_argument('--proxy-server=http://myproxy:port')

-1
投票

参考https://github.com/SeleniumHQ/selenium/issues/4477 添加以下代码

self.chrome_options = webdriver.ChromeOptions()
self.chrome_options.add_argument("--window-size=1920,1080")
self.chrome_options.add_argument("--disable-extensions")
self.chrome_options.add_argument("--proxy-server='direct://'")
self.chrome_options.add_argument("--proxy-bypass-list=*")
self.chrome_options.add_argument("--start-maximized")
self.chrome_options.add_argument('--headless')
self.chrome_options.add_argument('--disable-gpu')
self.chrome_options.add_argument('--disable-dev-shm-usage')
self.chrome_options.add_argument('--no-sandbox')
self.chrome_options.add_argument('--ignore-certificate-errors')
self.browser = webdriver.Chrome(options=self.chrome_options)
© www.soinside.com 2019 - 2024. All rights reserved.