硒可与Chrome一起使用,但不能与无头Chrome一起使用 [英] Selenium working with Chrome, but not headless Chrome

查看:88
本文介绍了硒可与Chrome一起使用,但不能与无头Chrome一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Selenium和最初的PhantomJS开发了一些Python脚本.在走向自动下载时,我改用了(带头的)Firefox(正常工作),然后选择了无头选项的Chrome,这样我就不会打开浏览器了.

I've developed a couple of Python scripts using Selenium and, at first, PhantomJS. While heading toward automated downloads, I switched for (headed) Firefox (which worked) and then Chrome with the headless option so I won't have the browser opening in front of me.

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

My first script, which accesses a page and a couple of HTML elements, works perfectly with headless Chrome.

第二个,仅适用于带头的Chrome .如果添加无头"选项,它将不再起作用.当我尝试以无头模式打印HTML以查看为什么找不到所需的HTML元素时,我所拥有的只是:

The second one, however, works only with headed Chrome. If I add the "headless" option, it doesn't work anymore. When I try to print the HTML in headless mode to see why it cannot find the HTML element I'm looking for, all I have is :

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

使用Chrome浏览器,我可以打印出完整的HTML. 这就是我启动无头Chrome的方式:

With the headed Chrome, I have a complete HTML printed. This is how I start my headless Chrome :

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

同样,请注意,这在我的另一个脚本中有效.唯一的区别是我需要登录才能访问该页面,但是即使那样,为什么它也可以与头部配合使用?通过填写表格,我的脚本可以自动登录.

Again, note that this works in another of my script. The only difference here is that I need to log in to access the page, but even then, why would it work with the head? My script is made to log in automatically anyway by filling the form.

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

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

有什么主意吗? 谢谢.

Any idea? Thanks.

**这是代码的开头**

** Here is the beginning of the code**

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()

推荐答案

我和您一样有相同的经历,并通过使用xvfb和pyvirtualdisplay解决了该问题.

I had a same experience like you, and solved it by using xvfb and pyvirtualdisplay.

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

I use chromedrive=v2.3.1, chrome-browser=v60 and Selenium=3.4.3

在无头Chrome中,某些脚本似乎无法按预期工作.

In Headless chrome, some of script seems not to work as expected.

请参阅 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必须使用"pyvortualdisplay"

xvfb is required to use "pyvortualdisplay"

$ sudo apt-get install -y xvfb 

这篇关于硒可与Chrome一起使用,但不能与无头Chrome一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆