How can I use Firefox webdriver in pythonanywhere.com - python

I can use Chromedriver easyly in pythonanywhere.com but how can I use Firefox driver in pythonanywhere's python script ?
this is how I use chromedriver
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
browser = webdriver.Chrome(options=chrome_options)
browser.get("https://www.youtube.com/")
print("Done")
browser.quit()

PythonAnywhere dev here: unfortunately Firefox doesn't work well enough to be fully supported on PythonAnywhere now -- it's too resource-intensive. Chrome is more lightweight, so it's the only Selenium option we support.
(Interestingly, it used to be the other way around -- Firefox was more lightweight, and was the one that we supported. But the browsers have changed over time, so we have to change with them.)

Related

How to stop selenium to download chromedriver everytime

I'm always using the following code to initiate my selenium web driver. But the problem is, it always tries to download the latest chrome driver. How can I prevent this behavior and just let it update if there is any update on the real chrome browser?
options = Options()
options.add_argument("--start-maximized")
driver = webdriver.Chrome(service=Service(ChromeDriverManager(cache_valid_range=0).install()), options=options)
you can download chromedriver for your os and right version from https://chromedriver.chromium.org/downloads
and pass the absolute path to executable path below.
driver = webdriver.Chrome(executable_path =r"C:/path/to/chromedriver.exe", options=options)
This will not download the driver again
You could use chromedriver-autoinstaller
import chromedriver_autoinstaller
chromedriver_autoinstaller.install()
After this code then add your code after
options = Options()
options.add_argument("--start-maximized")
driver = webdriver.Chrome(options=options)

Chromedriver driver.get() working on Mac but does work on Windows (Chrome version 101.0.4951.54)

I am writing a program that uses selenium and chromedriver to load a page. The same code loads the page (nytimes.com) on my Windows computer but not on my Mac. On my Mac, it loads the webdriver with the blank data:, page but just stops and the console log just shows it waiting. I don't know why the driver does not get the page.
This is my code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
opts = Options()
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('http://nytimes.com')
html = driver.page_source
This below is the last thing it shows in the console log. It just waits there after that with the blinking cursor.
====== WebDriver manager ======
Current google-chrome version is 101.0.4951
Get LATEST chromedriver version for 101.0.4951 google-chrome
Driver [/Users/me/.wdm/drivers/chromedriver/mac64_m1/101.0.4951.41/chromedriver]
found in cache
testing.py:14: DeprecationWarning: executable_path has been deprecated, please pass
in a Service object
driver = webdriver.Chrome(ChromeDriverManager().install())
What could be the problem? I have a suspicion that it's the new version of chrome that I'm using but why would that change anything?
From the below message it seems you executed code before which installed older version of driver. Now when you trying to run code again , it detecting older version
in cache. Please check below path once.
Driver [/Users/me/.wdm/drivers/chromedriver/mac64_m1/101.0.4951.41/chromedriver]
found in cache

Python, PhantomJS says I am not using headless?

my code is:
from selenium import webdriver
driver = webdriver.PhantomJS(executable_path='driver/bin/phantomjs.exe')
driver.get("https://www.test.com")
print(driver.current_url)
It seems to run fine but before it runs I always get this error:
UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless
Why am I getting this error? I thought my PhantomJS was headless as it still works and no browser pops-up is this error save to ignore?
Selenium considers PhantomJS as deprecated, so you need to us either Chrome or Firefox in headless mode.
Here are the steps to use Chrome in headless mode:
download chrome driver from https://sites.google.com/a/chromium.org/chromedriver/getting-started
extract it to a folder
add this folder to your PATH environment variable (if you don't do it, you will have to use webdriver.Chrome('/your/path/to/chromedriver') in the code below instead of webdriver.Chrome())
Then use it like this:
from selenium import webdriver
# prepare the option for the chrome driver
options = webdriver.ChromeOptions()
options.add_argument('headless')
# start chrome browser
browser = webdriver.Chrome(chrome_options=options)
browser.get('http://www.google.com/xhtml')
print(browser.current_url)
browser.quit()
More on how to use ChromeDriver
For the other options: here (also here and here)
In Selenium 3.8.1 PhantomJS marked as deprecated webdriver and recommend us to use either Chrome or Firefox in headless mode.
You could use this:
from selenium import webdriver
browser = webdriver.Chrome('chromedriver_path/chromedriver')
browser.get("https://www.test.com")
print(browser.current_url)
browser.quit()
Found an alternative you can add options.add_argument('headless') to chrome

chromedriver in headless mode

I am using chromedriver for web scraping by giving the binary path.
driver = webdriver.Chrome(r"C:\Program Files\JetBrains\PyCharm Community Edition 2017.3.3\bin\chromedriver.exe")
driver.get("https://www.example.com/")
This invoke chromedriver in GUI mode. How can I start chrome in headless mode?
Chrome headless is ideally much better than PhantomJS, whose owner decided to stop maintaining the project because the arrival of Chrome headless made it a bit less necessary. That being said, if you have a Chrome version which supports headless, you can do this:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('headless')
driver = webdriver.Chrome(chrome_options=options)
As you see, headless is an argument, so if for some reason you want to run the same code but you need to see the GUI, remove that argument.
By the way, if you ever wanted to give the binary location, a nice way to do it is also with options:
options.binary_location = 'path to your chrome binary'
But if your installed version is recent enough, there should be no reason to do so.

Selenium-Python with chromium browser (windows)

I am trying to launch chromium browser with selenium python on windows 8.
Added binary_location as chromium binary location which is appdata.
But still chromedriver starts google chrome instead of chromium.
If I uninstall google chrome, then chromedriver by default launches chromium. But with chrome installed it always launches chrome regardless.
Does anyone have an idea on how to start chromium with selenium while chrome installed?
Please do not mark it as duplicate. The other one was about unix and solution provided to selenium java while this one is about windows and python.
Try this:
from selenium.webdriver.chrome.options import Options
options = Options()
options.binary_location = r"D:\.....\chrome.exe"
# This line defines your Chromium exe file location.
driver = webdriver.Chrome(chrome_options=options)
driver.get('https://www.google.com/')
Worked for me.
I installed both Chrome and Chromium.
It starts the specified exe.
To start Chromium browser you can use the following code block :
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.binary_location("C:\\path\\to\\chrome.exe") //path to chromium binary
options.add_argument("start-maximized")
options.add_argument("--disable-gpu-vsync") //only for Windows
options.add_argument("--remote-debugging-port=9222") //for MAC & Linux
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
driver.get('http://google.com/')
Note : Here you can find more details about Run Chromium with flags

Categories

Resources