no code runs after defining webdriver in selenium - python

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
options = Options()
options.add_argument("start-maximized")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
print("hello world")
running python 3.10 on linux ubuntu
I was trying to play aruond with selenium however any code that I write after defining the webdriver does not work.
it seems that the interpreter gets stuck after defining the webdriver. it opens the webdriver but only shows "data:," and nothing else and won't respond to any code written after.

Chromium support is limited on selenium in my experience.
Please look at the replies here which do mention chromium-driver and how to get it
sudo apt-get install chromium-chromedriver
Should get you chromium driver and they have some example code there that doesn't look too outdated.

I figured out the problem, I only had chromium installed on my computer and I assumed that the chromium drivers and the chrome drivers are the same thing(still can't find any refrence to the chromium drivers)

Related

Running selenium issue

I'm new to selenium just started learning it using freecodecamp and started using selenium
downloaded selenium using the following command in python
pip install selenium
And I manually installed chrome web driver.
Written the code as:
import os
from selenium import webdriver
os.environ["PATH"] +=r"C:/seleniumDrivers" #where the chrome driver is installed
driver = webdriver.Chrome()
The following error occured when I run this:
line 81, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be
in
PATH. Please see https://chromedriver.chromium.org/home
But if I modified code a bit:
import os
from selenium import webdriver
driver = webdriver.Chrome("C:/seleniumDrivers/chromedriver.exe)
A pop website appeared and closes immediately.
Can anyone please suggest what should I do.
In the newer selenium, including the webdriver is depreciated. Use ChromeDriverManager instead of referencing the path to chromedriver.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import Select
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(ChromeDriverManager().install())
For the first issue
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://chromedriver.chromium.org/home
You have to point the PATH to the correct directory, which containing the chromedriver.exe executable on the first level (i.e. you can see the executable when you open the path, it usually named bin)
For the second issue
A pop website appeared and closes immediately. Can anyone please suggest what should I do.
It is because the code has finished (no further code to execute), so the program exits and closes the browser. You can try adding input() after the last line to see if the browser remains open (until you press enter on the command line).
You don't need to use the os module unless this is specific to your script, you can save the path as a variable. I'm using Selenium currently and this should work for you - you need to specify the "Service" now in the webdriver. Do the 2 pip installs first in terminal.
# pip install selenium #
# pip install webdriver-manager #
from selenium.webdriver.chrome.service import Service
from selenium import webdriver
service = Service("C:/seleniumDrivers/chromedriver.exe")
driver = webdriver.Chrome(service=service)
driver.get(URL)

[WinError 2]FileNotFoundError in python Selenium webDriver

I'm started to learn python Selenium. I just downloaded by pip pip install python-selenium and started to write first lines and at this moment appeares
>>> from selenium import webdriver
>>> from selenium.webdriver.common.keys import Keys
>>> driver = webdriver.Firefox()
[WinError2] FileNotFoundError...
...
Then I tried it with Chrome but appeares the same thing. Who can solve this problem?
You do following things and check whether it works.
Upgrade python bindings using
pip install -U selenium
for chrome download the latest chrome driver "ChromeDriver 2.45"
and write the code
from selenium import webdriver
driver=webdriver.Chrome("Path of the Chromedriver" + "chromedriver.exe" )

How can I choose which firefox executable to use among multiple firefox binaries through Python selenium

I'm using python (v 3.65) selenium (v3.11.0) on Mac OS X (v 10.11.6) with firefox (v 59.0.3) and geckodriver (v 0.20.1). I have my usual firefox in the Applications folder and a second firefox in another folder. How can I tell python selenium to use the second firefox instead of going to the one in Applications?
I'd prefer an answer that generalises to other browsers besides firefox/geckodriver, if possible.
To choose and use one of the Firefox executable among multiple you can use the argument binary_location from firefox.options. As an example in the following code block I have used the Firefox Nightly binary to open the Firefox Nightly browser :
Code Block :
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.binary_location = r'C:\Program Files\Firefox Nightly\firefox.exe'
driver = webdriver.Firefox(firefox_options=options, executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get('http://google.com/')
print("Firefox Browser Invoked")
driver.quit()
Console Output :
Firefox Browser Invoked

selenium 3.6.0 and extensions

Note that I edited the question.
How to activate blocking for ghostery extension in firefox through selenium python bindings? Note that by default the blocking in ghostery is disabled.
I am using selenium 3.6.0 and ghostery 7.3.3.7. Note that I am using selenium + pull request #4790 that allow the new web extensions, because the released version lack this feature.
I do the following:
from selenium import webdriver
fp = webdriver.FirefoxProfile()
fp.add_extension(extension=/home/nsarafij/ghostery-7.3.3.7.xpi)
driver = webdriver.Firefox(firefox_profile = fp)
Is it possible to activate blocking in ghostery through setting preferences?
fp.set_preferances(...)
Or, I have to do something different?
I am not sure i understand you correctly - you want to add Ghostery to Firefox right?
If so, this should work:
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
firefoxProfile = FirefoxProfile()
ghostery="PATH_TO_GHOSTERY_ADDON"
firefoxProfile.add_extension(ghostery)
firefoxProfile.update_preferences()
driver = webdriver.Firefox(firefoxProfile)

Python Selenium Firefox driver - Disable Images

Before I have used the code below, but it doesn't work anymore with firefox update.
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
firefoxProfile = FirefoxProfile()
firefoxProfile.set_preference('permissions.default.image', 2)
I also tried this one below, it seems good but is there a way to disable images without add-on or 3rd party tools?
from selenium import webdriver
firefox_profile = webdriver.FirefoxProfile()
firefox_profile.add_extension(folder_xpi_file_saved_in + "\\quickjava-2.0.6-fx.xpi")
firefox_profile.set_preference("thatoneguydotnet.QuickJava.startupStatus.Images", 2) ## Turns images off
Have you tried updating your selenium after the Firefox update?
eg :
sudo -H pip install --upgrade selenium

Categories

Resources