unable to call firefox from selenium in python on AWS machine

前端 未结 4 414
别那么骄傲
别那么骄傲 2020-11-28 23:17

I am trying to use selenium from python to scrape some dynamics pages with javascript. However, I cannot call firefox after I followed the instruction of selenium on the pyp

相关标签:
4条回答
  • 2020-11-28 23:20

    This is already in the comment of OP's question, but to lay it out as an answer. You can have Selenium run in the background without opening an actual browser window.

    For example, if you use Chrome, set these options:

    from selenium.webdriver.chrome.options import Options
    chrome_options = Options()
    chrome_options.set_headless(headless=True)
    

    Then when you call your web driver, your settings become a parameter:

    browser = webdriver.Chrome(chrome_options=chrome_options)
    
    0 讨论(0)
  • 2020-11-28 23:24

    I too had faced same problem.I was on Firefox 47 and Selenium 2.53. So what I did was downgraded Firefox to 45. This worked.

    1) Remove Firefox 47 first :

    sudo apt-get purge firefox

    2) Check for available versions:

    apt-cache show firefox | grep Version

    It will show available firefox versions like:

    Version: 47.0+build3-0ubuntu0.16.04.1

    Version: 45.0.2+build1-0ubuntu1

    3) Tell which build to download

    sudo apt-get install firefox=45.0.2+build1-0ubuntu1

    4) Next you have to not upgrade to the newer version again.

    sudo apt-mark hold firefox

    5) If you want to upgrade later

    sudo apt-mark unhold firefox sudo apt-get upgrade

    Hope this helps.

    0 讨论(0)
  • 2020-11-28 23:28

    For Debian 10 and Ubuntu 18.04 this is a complete running example:

    1. Download the Chrome driver in ~/Downloads:
      $ wget https://chromedriver.storage.googleapis.com/80.0.3987.16/chromedriver_linux64.zip

    2. Unpack it with unzip chromedriver_linux64.zip

    3. Move the file to an executable folder (already with a path):
      $ sudo mv chromedriver /usr/local/bin

    Then run this code in a notebook with Jupyter or within a a script:

    from selenium.webdriver import Chrome
    from selenium.webdriver.chrome.options import Options
    
    chrome_options = Options()
    chrome_options.set_headless(headless=True)
    
    
    browser = Chrome(chrome_options=chrome_options)
    browser.get('http://www.linkedin.com/')
    print(browser.page_source)
    

    This will print the whole source HTML in the page.

    0 讨论(0)
  • 2020-11-28 23:42

    The problem is Firefox requires a display. I've used pyvirtualdisplay in my example to simulate a display. The solution is:

    from pyvirtualdisplay import Display
    from selenium import webdriver
    
    display = Display(visible=0, size=(1024, 768))
    display.start()
    
    driver= webdriver.Firefox()
    driver.get("http://www.somewebsite.com/")
    
    <---some code--->
    
    #driver.close() # Close the current window.
    driver.quit() # Quit the driver and close every associated window.
    display.stop()
    

    Please note that pyvirtualdisplay requires one of the following back-ends: Xvfb, Xephyr, Xvnc.

    This should resolve your issue.

    0 讨论(0)
提交回复
热议问题