selenium.webdriver.chrome.service gives error message "Module not found" - selenium

I am using the code in PyCharm as suggested here: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
but when I do that, I get an error message saying selenium module not found. But the first line of code works, which is also based on the selenium module. Also, I have added selenium as a Python interpreter. I am totally new to this.
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.Chrome.service import Service
s = Service('C:\\Users\\...\\chromedriver.exe')
driver = webdriver.Chrome(service=s)

The module is selenium.webdriver.chrome.service
You need to replace the C (in uppercase) with c (in lowercase) and the effective line of import will be:
from selenium.webdriver.chrome.service import Service

Related

Issue with Selenium installation

I'm facing the following problem.
I'm trying to execute the code:
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.options import Options
And when I try to run it, the following error is returned:
Exception has occurred: ImportError cannot import name 'Options' from 'selenium.webdriver.common.options'
I've checked other threads but none of them helped me.
I uninstalled selenium and installed again. Also, I double-checked to see if my file name wasn't selenium.py.
Can someone help me on that?
Thank you.
This does not seem like an installation issue.
I presume you mean to write this.
from selenium.webdriver.chrome.options import Options
or
from selenium.webdriver.firefox.options import Options

Selenium: get() not working with custom google profile

All what im trying to do is pretty much access whatsapp web where I have my whatsapp already linked, However when I use a custom profile the profile does open, however browser.get("https://web.whatsapp.com) doesn't seem to open. or any browser.get(). What could be the issue?
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.common.exceptions import NoSuchElementException, TimeoutException, WebDriverException
options = webdriver.ChromeOptions()
options.add_argument('--user-data-dir=/Users/omarassouma/Library/Application Support/Google/Chrome/')
options.add_experimental_option("deatch", True)
browser = webdriver.Chrome(executable_path="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",chrome_options=options)
browser.get("https://web.whatsapp.com/")
this is the updated version, it now opens whatsapp web however not in a custom profile, moreover I cant really use webdriver.options(), is there anything extra I have to import?.
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=/Users/omarassouma/Library/Application Support/Google/Chrome/User Data/Default")
browser = webdriver.Chrome(executable_path="/Users/omarassouma/Downloads/chromedriver",options=options)
browser.get("https://web.whatsapp.com/")
You need to take care of a couple of things as follows:
To use a Custome Chrome Profile you have to pass the absolute path as follows:
options.add_argument("user-data-dir=/Users/omarassouma/Library/Application Support/Google/Chrome/User Data/Default")
You can find a detailed discussion in How to use Chrome Profile in Selenium Webdriver Python 3
Instead of passing the absolute path of the google-chrome binary, you need to pass the absolute path of the ChromeDriver through the key executable_path.
Additionally, instead of chrome_options you need to use options as chrome_options is deprecated now.
You can find a detailed discussion in DeprecationWarning: use options instead of chrome_options error using Brave Browser With Python Selenium and Chromedriver on Windows
So effectively the line of code will be:
browser = webdriver.Chrome(executable_path="/path/to/chromedriver", options=options)
At 05.11.2022 I found the only way to pass through authorization for myself is using cookie - https://stackoverflow.com/a/15058521
Runing selenium driver with google account isn't working

Error: DeprecationWarning: executable_path has been deprecated, please pass in a Service object [duplicate]

This question already has answers here:
DeprecationWarning: executable_path has been deprecated selenium python
(10 answers)
DeprecationWarning: executable_path has been deprecated, please pass in a Service object
(1 answer)
Closed last year.
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome("./chromedriver.exe")
driver.get("https://www.youtube.com/")
search = driver.find_element(By.ID, "search").click()
search.send_keys("test")
search.send_keys("RETURN")
This is my code that is meant search test on youtube however when I run it youtube opens and I get an error saying "DeprecationWarning: executable_path has been deprecated, please pass in a Service object."
My chromedriver is in the same folder as my python file.
Try this, it should fix your problem:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
ser = Service("./chromedriver.exe")
driver = webdriver.Chrome(service=ser)
driver.get("https://www.youtube.com/")
search = driver.find_element(By.ID, "search").click()
search.send_keys("test")
search.send_keys("RETURN")

How to use undetected_chromedriver.v2 in kali linux

How to use undetected_chromedriver.v2 in kali linux
import undetected_chromedriver.v2 as uc
options = uc.ChromeOptions()
# setting profile
options.user_data_dir = "c:\\temp\\profile"
# another way to set profile is the below (which takes precedence if both variants are used
options.add_argument('--user-data-dir=c:\\temp\\profile2')
# just some options passing in to skip annoying popups
options.add_argument('--no-first-run --no-service-autorun --password-store=basic')
bw = uc.Chrome(options=options, version_main=92)
If you are on selenium4 then using undetected_chromedriver.v2 you will face an error as:
TypeError: __init__() got an unexpected keyword argument 'service'
which is inline as per the status:
July 2021: Currently busy implementing selenium 4 for undetected-chromedriver
However with selenium4 you can still use the undetected_chromedriver v1 as follows:
import undetected_chromedriver as uc
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
options = uc.ChromeOptions()
options.add_argument("start-maximized")
s = Service('C:\\BrowserDrivers\\chromedriver.exe')
driver = uc.Chrome(service=s, options=options)
driver.get('https://datadome.co/customers-stories/toppreise-ends-web-scraping-and-content-theft-with-datadome/')
driver.save_screenshot('datadome_undetected_webddriver.png')
References
You can find a couple of relevant detailed discussions in:
Undetected Chromedriver not loading correctly
Evade detection of selenium automation

Chromedriver is not being found: executable needs to be in PATH

I have a windows64 bit machine and I downloaded the chromedriver on the exact location it is supposed to be and my file path is this:
'C:\Users\username\AppData\Local\Google\Chrome\Application\chrome.exe\chromedriver'
Then I wrote this code:
import selenium
from selenium import webdriver
driver=webdriver.Chrome('C:\Users\pushp\AppData\Local\Google\Chrome\Application\chrome.exe\chromedriver')
However, I am getting a file not found error and also this message -
'Message: 'chromedriver' executable needs to be in PATH.'
How do I fix this?
driver =webdriver.Chrome(r'C:\Users\pushp\AppData\Local\Google\Chrome\Application\chrome.exe\chromedriver.exe')
Just need to add .exe
Set it like the following
import selenium
from selenium import webdriver
webdriver.Chrome(executable_path=r"C:\Users\pushp\AppData\Local\Google\Chrome\Application\chromedriver.exe")
Or
webdriver.Chrome(executable_path="C:\\Users\\pushp\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe")
You can also install pip install webdriver-manager then run the following code which will install the correct webdriver for you
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())