【问题标题】:Headless Chrome Navigate to Page, click to linked page, then printToPDFHeadless Chrome 导航到页面,点击链接页面,然后 printToPDF
【发布时间】:2018-01-03 18:08:12
【问题描述】:

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

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

这是我目前的代码:

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 并拥有 chrome 版本 60,我应该使用 canary 吗?感谢您对此提供的任何帮助。

【问题讨论】:

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


    【解决方案1】:
    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)希望这会有所帮助!

    【讨论】:

    • 我刚刚尝试编辑我发布的代码以使用这些选项,但现在出现错误。你能把文件打印成pdf吗?您介意发布您使用的其余代码吗?我开始认为这是我的代理有问题,或者 PyChromeDevTools 和 Selenium 使用相同的端口并且不能一起运行。
    • 尝试在代码中注释这两行:chrome_options.add_argument('--remote-debugging-port=9222') chrome_options.add_argument('--disable-gpu')
    • 不幸的是,将这些注释掉并没有解决我的问题。接下来我将尝试在我的个人计算机上复制它,并排除我的工作防火墙是否妨碍了我。我能够让它与 FireFox 一起使用,但防火墙迫使我登录每个循环(IT 强烈鼓励 IE,但我敢打赌他们都没有使用它......)我会在完成后发布我的问题/感谢。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多