Selenium WebDriver commen - python

Here is the python code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
import subprocess
EXTENSION_PATH = "C:/Users/HP/AppData/Local/Google/Chrome/User Data/Default/Extensions/nkbihfbeogaeaoehlefnkodbefgpgknn/10.17.0_0.crx"
opt = webdriver.ChromeOptions()
opt.add_extension(EXTENSION_PATH)
driver = webdriver.Chrome(chrome_options=opt)
time.sleep(1)
I try to run the program I get the following error:(file name: 1.py)
"C:/Program Files/Python310/python.exe" c:/Users/HP/Desktop/py/1.py
c:\Users\HP\Desktop\py\1.py:27: DeprecationWarning: use options instead of chrome_options
driver = webdriver.Chrome(chrome_options=opt)
Traceback (most recent call last):
File "C:\Users\HP\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\common\service.py", line 71, in start
self.process = subprocess.Popen(cmd, env=self.env,
File "C:\Program Files\Python310\lib\subprocess.py", line 969, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Program Files\Python310\lib\subprocess.py", line 1438, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\Users\HP\Desktop\py\1.py", line 27, in <module>
driver = webdriver.Chrome(chrome_options=opt)
File "C:\Users\HP\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\chrome\webdriver.py", line 69, in __init__
super().__init__(DesiredCapabilities.CHROME['browserName'], "goog",
File "C:\Users\HP\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\chromium\webdriver.py", line 89, in __init__
self.service.start()
File "C:\Users\HP\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\common\service.py", 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
I put all the required files in the patch.
I am using a windows

Change:
driver = webdriver.Chrome(chrome_options=opt)
with:
driver = webdriver.Chrome(executable_path=r"C:\path\to\chromedriver.exe", options=opt)
You should be good to go.
Your effective code block will be:
EXTENSION_PATH = "C:/Users/HP/AppData/Local/Google/Chrome/User Data/Default/Extensions/nkbihfbeogaeaoehlefnkodbefgpgknn/10.17.0_0.crx"
opt = webdriver.ChromeOptions()
opt.add_extension(EXTENSION_PATH)
driver = webdriver.Chrome(executable_path=r"C:\path\to\chromedriver.exe", options=opt)

Related

Creating new python Class with Selenium webdriver

Good morning.
I'm new to Selenium and I have some issues with Selenium 4.
Basically I'm trying to create a class that will have some custom methods like sending a request to specified URL.
But...I'm stuck at init method.
With the code I wrote the browser is starting but it breaks with error message:
====== WebDriver manager ====== Current google-chrome version is 101.0.4951 Get LATEST chromedriver version for 101.0.4951 google-chrome Driver
[/home/agent2/.wdm/drivers/chromedriver/linux64/101.0.4951.41/chromedriver]
found in cache Traceback (most recent call last): File
"/home/agent2/1-Python/Selenium-Scrapy/venv/lib/python3.10/site-packages/selenium/webdriver/common/service.py",
line 71, in start
self.process = subprocess.Popen(cmd, env=self.env, File "/usr/lib/python3.10/subprocess.py", line 966, in init
self._execute_child(args, executable, preexec_fn, close_fds, File "/usr/lib/python3.10/subprocess.py", line 1842, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename) FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"/home/agent2/1-Python/Selenium-Scrapy/bot/booking/booking.py", line
29, in
a = MyBot() File "/home/agent2/1-Python/Selenium-Scrapy/bot/booking/booking.py", line
17, in init
super(MyBot, self).init() File "/home/agent2/1-Python/Selenium-Scrapy/venv/lib/python3.10/site-packages/selenium/webdriver/chrome/webdriver.py",
line 70, in init
super(WebDriver, self).init(DesiredCapabilities.CHROME['browserName'], "goog",
File
"/home/agent2/1-Python/Selenium-Scrapy/venv/lib/python3.10/site-packages/selenium/webdriver/chromium/webdriver.py",
line 90, in init
self.service.start() File "/home/agent2/1-Python/Selenium-Scrapy/venv/lib/python3.10/site-packages/selenium/webdriver/common/service.py",
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
This is my code:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
class MyBot(webdriver.Chrome):
"""My class"""
def __init__(
self,
driver=webdriver.Chrome(
service=Service(ChromeDriverManager().install()),
options = webdriver.ChromeOptions()
)
):
self.driver = driver
super(MyBot, self).__init__()
def land_first_page(self):
"""My custom method"""
self.get('My-url')
a = MyBot()
a.land_first_page()
To be honest I don't know how to solve this... Found some solutions but with Selenium 3 where you could simply pass a path to driver location.
Thanks!
This is because your class inherits from webdriver.Chrome, WebDriver __init__ tries to start a service using the chromedriver.exe file, but since you didn't provide a path it uses the default value executable_path="chromedriver" which doesn't exists in your project.
Just remove the inheritance and use the driver instance ChromeDriverManager().install() creates for you.

I facing a problem in set up selenium in virtual studio code for python

from selenium import webdriver
chromedriver_location = "/Users/Sony/Desktop/XUAN/Python/chromedriver_win32/chromedriver"
driver = webdriver.Chrome()
driver.get('https://sapsnkra.moe.gov.my/ibubapa2/')
input(" ")
PS C:\Users\Sony\Desktop\XUAN\Python> python -u "c:\Users\Sony\Desktop\XUAN\Python\Python Project\tempCodeRunnerFile.py"
Traceback (most recent call last):
File "C:\Users\Sony\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\selenium\webdriver\common\service.py", line 71, in start
self.process = subprocess.Popen(cmd, env=self.env,
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2544.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 951, in init
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2544.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 1420, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\Users\Sony\Desktop\XUAN\Python\Python Project\tempCodeRunnerFile.py", line 3, in
driver = webdriver.Chrome()
File "C:\Users\Sony\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\selenium\webdriver\chrome\webdriver.py", line 70, in init
super(WebDriver, self).init(DesiredCapabilities.CHROME['browserName'], "goog",
File "C:\Users\Sony\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\selenium\webdriver\chromium\webdriver.py", line 90, in init
self.service.start()
File "C:\Users\Sony\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\selenium\webdriver\common\service.py", 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
You can actually use webdriver-manager to automatically manage your chromedriver version.
You can use it like this:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://sapsnkra.moe.gov.my/ibubapa2/')
input(" ")

Cant run Selenium on Ubuntu with Python

I cant run or install selenium on Ubuntu 18.04.3 ...
I followed this tutorial: https://medium.com/#hoppy/how-to-test-or-scrape-javascript-rendered-websites-with-python-selenium-a-beginner-step-by-c137892216aa also with the digitalocean setup.
I tried different things...
I installed the geckodriver in /var/bin
in /var/local/bin
added both to PATH
tried with a path in the code and without
tried with firefox (preferred) ...
nothing helps
This Code:
import time
from selenium import webdriver
driver = webdriver.Chrome('/usr/bin/chromedriver') # Optional argument, if not specified will search path.
driver.get('http://www.google.com/');
time.sleep(5) # Let the user actually see something!
search_box = driver.find_element_by_name('q')
search_box.send_keys('ChromeDriver')
search_box.submit()
time.sleep(5) # Let the user actually see something!
driver.quit()
Ends up with:
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
selenium.common.exceptions.WebDriverException:: command not found
(venv) root#EtherScrape:/var/test# python /var/test/server_test.py Traceback (most recent call last):
File "/var/test/server_test.py", line 5, in <module>
driver = webdriver.Chrome('/usr/bin/chromedriver') # Optional argument, if not specified will search path.
File "/var/venv/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
desired_capabilities=desired_capabilities)
File "/var/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 92, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/var/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 179, in start_session
response = self.execute(Command.NEW_SESSION, capabilities)
File "/var/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "/var/venv/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
(Driver info: chromedriver=83.0.4103.61 (94f915a8d7c408b09cc7352161ad592299f384d2-refs/branch-heads/4103#{#561}),platform=Linux 4.15.0-66-generic x86_64)
This Code:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.headless = True
driver = webdriver.Firefox(firefox_options=options)
With that error:
Traceback (most recent call last):
File "/var/venv/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 64, in start
stdout=self.log_file, stderr=self.log_file)
File "/usr/lib/python3.6/subprocess.py", line 729, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.6/subprocess.py", line 1364, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver': 'geckodriver'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/var/test/server_test.py", line 6, in <module>
driver = webdriver.Firefox(firefox_options=options)
File "/var/venv/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 135, in __init__
self.service.start()
File "/var/venv/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 71, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x7ff3109ced68>>
Traceback (most recent call last):
File "/var/venv/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 163, in __del__
self.stop()
File "/var/venv/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 135, in stop
if self.process is None:
AttributeError: 'Service' object has no attribute 'process'
Please help.
Tried many solutions so far but no one worked out...
options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
options.addArguments("--no-sandbox"); // Bypass OS security model
Works for me at the chrome driver.

Error when importing webdriver from selenium

I'm trying to get all results from a predefined google search, but I'm stuck at the following error:
Traceback (most recent call last):
File "C:\python36\lib\site-packages\selenium\webdriver\common\service.py", line 74, in start
stdout=self.log_file, stderr=self.log_file)
File "C:\python36\lib\subprocess.py", line 707, in __init__
restore_signals, start_new_session)
File "C:\python36\lib\subprocess.py", line 992, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\amzar\Desktop\pyscripts\selen.py", line 23, in <module>
get_results("dog")
File "C:\Users\amzar\Desktop\pyscripts\selen.py", line 6, in get_results
browser = webdriver.Firefox()
File "C:\python36\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 142, in __init__
self.service.start()
File "C:\python36\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
I checked for 30 minutes all possibilities with importing, but I can't figured it out.
from selenium import webdriver
def get_results(search_term):
url = "http://startpage.com"
browser = webdriver.Firefox()
browser.get(url)
search_box = browser.find_element_by_id("query")
search_box.send_keys(search_term)
search_box.submit()
try:
links = browser.find_elements_by_xpath("/ol[#class='web_regular_results']//h3//a")
except:
links = browser.find_elements_by_xpath("//h3//a")
results = []
for link in links:
href = link.get_attribute("href")
printf(href)
results.append(href)
browser.close()
return results
get_results("dog")

Does selenium python bindings require firefox

Hi I just downloaded and installed selenium but, I can't figure out how to get it working I am using the following example as a test....
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox() #this is where I hit the error
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.send_keys("selenium")
elem.send_keys(Keys.RETURN)
assert "Google" in driver.title
driver.close()
I do not have firefox installed on my computer is this why it is giving me an error?
this is the error output I get when I try and run it
Traceback (most recent call last):
File "C:/Python27/test/helloworld.py", line 4, in <module>
driver = webdriver.Firefox()
File "C:\Python27\lib\site-packages\selenium-2.35.0-py2.7.egg\selenium\webdriver\firefox\webdriver.py", line 60, in __init__
self.binary, timeout),
File "C:\Python27\lib\site-packages\selenium-2.35.0-py2.7.egg\selenium\webdriver\firefox\extension_connection.py", line 47, in __init__
self.binary.launch_browser(self.profile)
File "C:\Python27\lib\site-packages\selenium-2.35.0-py2.7.egg\selenium\webdriver\firefox\firefox_binary.py", line 60, in launch_browser
self._start_from_profile_path(self.profile.path)
File "C:\Python27\lib\site-packages\selenium-2.35.0-py2.7.egg\selenium\webdriver\firefox\firefox_binary.py", line 83, in _start_from_profile_path
env=self._firefox_env).communicate()
File "C:\Python27\lib\subprocess.py", line 679, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 893, in _execute_child
startupinfo)
WindowsError: [Error 87] The parameter is incorrect
Following line starts Firefox browser so yes, it requires installed Firefox.
driver = webdriver.Firefox()
If you don't have Firefox installed you can use different browser instead:
webdriver.Opera
webdriver.Ie
webdriver.Chrome
...
See
help(webdriver)

Categories