代码之家  ›  专栏  ›  技术社区  ›  Gnougnou

硒与铬一起工作,但不是无头铬

  •  16
  • Gnougnou  · 技术社区  · 7 年前

    我使用Selenium开发了两个Python脚本,首先是PhantomJS。在走向自动下载的过程中,我选择了Firefox,然后选择了Chrome,选择了无头选项,这样我就不会让浏览器在我面前打开。

    我的第一个脚本可以访问一个页面和几个HTML元素,与无头Chrome完美结合。

    然而,第二个, 仅适用于头部镀铬

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

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

    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()
    
    7 回复  |  直到 5 年前
        1
  •  10
  •   frank    7 年前

    我和你有同样的经历,通过使用xvfb和pyvirtualdisplay解决了这个问题。

    请参阅vpassapera在中的评论 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()
    

    $ sudo apt-get install -y xvfb 
    
        2
  •  7
  •   Suraj Regmi    6 年前

    无头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")
    

    这就行了。

        3
  •  1
  •   mmichalik    7 年前

    在同一台机器上,无头chrome可能比有头chrome更快,请尝试在定位密码元素之前添加一些等待。

        4
  •  1
  •   lukashino    5 年前

    我的代码段:

    @pytest.fixture
    def chrome_options(chrome_options, pytestconfig):
        if pytestconfig.getoption('headless'):
            chrome_options.add_argument('--headless')
            chrome_options.add_argument("window-size=1920,1080")
        else:
            chrome_options.add_argument("start-maximized");
        return chrome_options
    
        5
  •  0
  •   HelloWorld123    5 年前

    对于一些人来说,如果你删除下面的内容,它会起作用。

    driver.fullscreen_window()
    
        6
  •  0
  •   Alan Alby    4 年前

    https://github.com/SeleniumHQ/selenium/issues/4477

    self.chrome_options = webdriver.ChromeOptions()
    self.chrome_options.add_argument("--window-size=1920,1080")
    self.chrome_options.add_argument("--disable-extensions")
    self.chrome_options.add_argument("--proxy-server='direct://'")
    self.chrome_options.add_argument("--proxy-bypass-list=*")
    self.chrome_options.add_argument("--start-maximized")
    self.chrome_options.add_argument('--headless')
    self.chrome_options.add_argument('--disable-gpu')
    self.chrome_options.add_argument('--disable-dev-shm-usage')
    self.chrome_options.add_argument('--no-sandbox')
    self.chrome_options.add_argument('--ignore-certificate-errors')
    self.browser = webdriver.Chrome(options=self.chrome_options)
    
        7
  •  0
  •   wisbucky Ishan    3 年前

    就我的情况而言,headless不起作用,因为我是一名代理人的幕后黑手。显然,Chrome可以使用系统代理,但headless不使用系统代理。

    我只需要提供代理,然后headless(以及Chrome)就可以工作了。

    options.add_argument('--proxy-server=http://myproxy:port')