无头Chrome导航到页面,单击链接页面,然后printToPDF

时间:2017-07-27 20:31:28

标签: python selenium google-chrome-devtools google-chrome-headless

我必须打印从产品信息页面链接到的所有html页面: http://prod.adv-bio.com/ProductDetail.aspx?ProdNo=1197

我已经设置了一个测试脚本,看看我是否可以只对其中一个html文件执行此操作。我使用PyChromeDevTools和Selenium,希望我可以在DevTools中使用printToPDF函数,同时使用Selenium点击我需要点击的链接。

这是我到目前为止的代码:

import requests
import time
import re
import websocket
import PyChromeDevTools
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options


chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--remote-debugging-port=9222')
chrome_options.add_argument('--window-size=1200x600')
chrome_options.add_argument('--disable-gpu')




driver = webdriver.Chrome('C:\\Users\\me\\AppData\\Local\\Programs\\Python\\Python36-    32\\selenium\\webdriver\\chromedriver_win32\\chromedriver.exe',         chrome_options = chrome_options)
driver.get("http://prod.adv-bio.com/ProductDetail.aspx?ProdNo=1197")

browser = PyChromeDevTools.ChromeInterface()
browser.Network.enable()
browser.Page.enable()
driver.implicitly_wait(20)                                                                 



driver.get("http://prod.adv-bio.com/ProductDetail.aspx?ProdNo=1197")
time.sleep(3)
url = driver.find_element_by_partial_link_text("Continuing")
time.sleep(3)
ActionChains(driver).click(url).perform()
time.sleep(3)


Page.printToPDF()

time.sleep(5)

我收到此错误

Traceback (most recent call last):
  File "C:\Users\me\SOtestfile.py", line 29, in <module>
    driver = webdriver.Chrome('C:\\Python27\\Lib\\site-
packages\\chromedriver_win32\\chromedriver.exe', chrome_options = 
chromeOptions, desired_capabilities=capabilities)
  File "C:\Python27\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 69, in __init__
desired_capabilities=desired_capabilities)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 98, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 188, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 256, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
WebDriverException: Message: chrome not reachable
  (Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 6.1.7601 SP1 x86_64)

我已经更新了chromedriver到2.31并且有60版铬,我应该使用金丝雀吗?我对此表示感谢。

1 个答案:

答案 0 :(得分:0)

from selenium.webdriver.common.desired_capabilities import 
DesiredCapabilities    

chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument("headless")
chromeOptions.add_argument("window-size=1366,768")

capabilities = DesiredCapabilities.CHROME.copy()
capabilities["browserName"] = "chrome"

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

在这里工作!! (Chrome 60 + Chromedriver 2.31)希望这会有所帮助!!