Gno*_*nou 13 python selenium google-chrome headless
我使用Selenium开发了一些Python脚本,最初是PhantomJS.在转向自动下载的同时,我切换到(朝向)Firefox(已经工作),然后使用无头选项切换Chrome,因此我不会在我面前打开浏览器.
我的第一个脚本访问一个页面和几个HTML元素,与无头Chrome完美配合.
然而,第二个只适用于Chrome.如果我添加"无头"选项,它将不再起作用.当我尝试在无头模式下打印HTML以查看为什么它找不到我正在寻找的HTML元素时,我只有:
<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html>
Run Code Online (Sandbox Code Playgroud)
使用标题Chrome,我有一个完整的HTML打印.这就是我开始无头Chrome的方式:
options = webdriver.ChromeOptions()
options.add_argument("--ignore-certificate-errors")
options.add_argument("headless")
driver = webdriver.Chrome(chrome_options=options)
Run Code Online (Sandbox Code Playgroud)
再次注意,这适用于我的另一个脚本.这里唯一的区别是我需要登录才能访问该页面,但即便如此,为什么它会与头部一起工作?填写表单,我的脚本无论如何都会自动登录.
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()
Run Code Online (Sandbox Code Playgroud)
小智 5
我有像你一样的经历,并通过使用xvfb和pyvirtualdisplay解决了它.
我使用chromedrive = v2.3.1,chrome-browser = v60和Selenium = 3.4.3
在Headless chrome中,一些脚本似乎没有按预期工作.
请参阅https://gist.github.com/addyosmani/5336747中的 vpassapera的评论.
怎么样尝试如下,
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()
Run Code Online (Sandbox Code Playgroud)
xvfb需要使用"pyvortualdisplay"
$ sudo apt-get install -y xvfb
Run Code Online (Sandbox Code Playgroud)
无头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")
Run Code Online (Sandbox Code Playgroud)
这样就可以了。
| 归档时间: |
|
| 查看次数: |
9748 次 |
| 最近记录: |