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
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)
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.
For Debian 10 and Ubuntu 18.04 this is a complete running example:
Download the Chrome driver in ~/Downloads:
$ wget https://chromedriver.storage.googleapis.com/80.0.3987.16/chromedriver_linux64.zip
Unpack it with unzip chromedriver_linux64.zip
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.
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.