Selenium working with Chrome, but not headless Chrome

后端 未结 6 736
渐次进展
渐次进展 2020-12-10 03:44

I\'ve developed a couple of Python scripts using Selenium and, at first, PhantomJS. While heading toward automated downloads, I switched for (headed) Firefox (which worked)

6条回答
  •  一向
    一向 (楼主)
    2020-12-10 04:20

    I had a same experience like you, and solved it by using xvfb and pyvirtualdisplay.

    I use chromedrive=v2.3.1, chrome-browser=v60 and Selenium=3.4.3

    In Headless chrome, some of script seems not to work as expected.

    Please refer to vpassapera's comment in https://gist.github.com/addyosmani/5336747.

    How about try it like below,

    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()
    

    xvfb is required to use "pyvortualdisplay"

    $ sudo apt-get install -y xvfb 
    

提交回复
热议问题