【问题标题】:Selenium screenshot_as_png not working using chrome on Ubuntu 18 [duplicate]Selenium screenshot_as_png 在 Ubuntu 18 上无法使用 chrome [重复]
【发布时间】:2020-06-14 21:32:14
【问题描述】:

我正在尝试使用 screenshot_as_png 方法截屏特定的 web_element,但出现错误。当我在 Windows 10 上运行此程序时一切正常,但 它在我的 AWS EC2 Ubuntu 18.04 上失败

ma​​in.py

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('--disable-notifications')
options.add_argument("headless")
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
options.add_argument("--disable-gpu")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--no-sandbox")

chrome = webdriver.Chrome(options=options)
chrome.get('http://stackoverflow.com/')

element = chrome.find_element_by_css_selector('.-main.grid--cell')
element.screenshot() #Error!
chrome.quit()

错误如下:

selenium.common.exceptions.WebDriverException: Message: unknown command: session/5c9bda106805d0d80a3f5c7b63dbf410/element/0.35398631812343306-1/screenshot

我正在使用的设置:

(Session info: headless chrome=80.0.3987.87)
(Driver info: chromedriver=2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706),platform=Linux 4.15.0-1060-aws x86_64)

【问题讨论】:

  • 如果可以,请尝试使用 Firefox。我在使用 Chrome 的 Mac 上遇到了同样的错误,但无论出于何种原因,Firefox 都能正常工作。

标签: python python-3.x selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

看来screenshot_as_png不是有效命令,

试试这个方法:

https://pythonspot.com/selenium-take-screenshot/

问候!

【讨论】:

    【解决方案2】:

    element 对象没有方法。它持有由方法driver.find_element_by_css_selector('.-main.grid--cell') 返回的元素的引用。因此出现错误。

    WebDriver 类中有一个方法叫save_screenshot("filename.extension")

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.binary_location = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"
    driver = webdriver.Chrome(options=options, executable_path="C:/Users/Downloads/chromedriver.exe", )
    
    driver.get('http://stackoverflow.com/')
    
    element = driver.find_element_by_css_selector('.-main.grid--cell')
    #element.screenshot_as_png() # there is no method on the element object hence the error
    driver.save_screenshot("screenshot.png")   # this is the method to get the screenshot
    
    driver.quit()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-12
      • 1970-01-01
      • 1970-01-01
      • 2019-08-04
      • 2020-10-07
      • 1970-01-01
      • 1970-01-01
      • 2012-10-19
      相关资源
      最近更新 更多