I am facing trouble launching the browser in selenium python - python

This is what I wrote below. I am using PyCharm 2022.2.2 (Community Edition)
Build #PC-222.4167.33, built on September 15, 2022
from selenium import webdriver
# Chrome driver = Path
#to open an URL in a BROWSER
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
name = "Trainer"
service_obj = Service(r"C:\Users\DS-02\Desktop\Manjit\chromedriver.exe") #---- doesn't works
driver = webdriver.Chrome(Service = service_obj)
The error which I am getting is below:
C:\Users\DS-02\AppData\Local\Programs\Python\Python310\python.exe C:\Users\DS-02\Desktop\Manjit\PythonSelenium\Class_1_demo_Browser.py
Traceback (most recent call last):
File "C:\Users\DS-02\Desktop\Manjit\PythonSelenium\Class_1_demo_Browser.py", line 10, in <module>
driver = webdriver.Chrome(Service = service_obj) #----- doesn't works
TypeError: WebDriver.__init__() got an unexpected keyword argument 'Service'
Please help,
Thanks and regards
I have tried the code below and works sometime now it doesn't works. I want to fix the code above:
#driver = webdriver.Chrome(executable_path = service_obj)
#driver = webdriver.Chrome(executable_path = '../Webdriver/chromedriver.exe')
Please help

You need to change this line driver = webdriver.Chrome(Service = service_obj) to be
driver = webdriver.Chrome(service = service_obj)
Parameter to be passed into webdriver.Chrome() in order to initialize driver object should be lowercased service while Service in your code is this:
selenium.webdriver.chrome.service, you called it Service here:
from selenium.webdriver.chrome.service import Service

This error message...
TypeError: WebDriver.__init__() got an unexpected keyword argument 'Service'
...implies that when the program execution starts the int() method counters an unexpected keyword argument 'Service'.
Details
With the availability of selenium4 the key executable_path() is deprecated and you have to use the key service.
However you need to take care of couple of things as follows:
Incase you use the service keyword you have to create a Service object and assign the absolute location of the ChromeDriver, you have to import:
from selenium.webdriver.chrome.service import Service
The keyword within webdriver.Chrome() is service. So you have to:
service_obj = Service('C:\\Users\DS-02\\Desktop\\Manjit\\chromedriver.exe')
driver = webdriver.Chrome(service = service_obj)

Related

TypeError: __init__() got an unexpected keyword argument 'service' how do I resolve this error in python? [duplicate]

I've been struggling with this problem for sometime, but now I'm coming back around to it. I'm attempting to use selenium to scrape data from a URL behind a company proxy using a pac file.
I'm using Chromedriver, which my browser uses the pac file in it's configuration.
I've been trying to use desired_capabilities, but the documentation is horrible or I'm not grasping something. Originally, I was attempting to webscrape with beautifulsoup, which I had working except the data I need now is in javascript, which can't be read with bs4.
Below is my code:
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
desired_capabilities = webdriver.DesiredCapabilities.CHROME.copy()
PAC_PROXY = {
'proxyAutoconfigUrl': 'http://proxy-pac/proxy.pac',
}
proxy = Proxy()
proxy.proxy_autoconfig_url = PAC_PROXY['proxyAutoconfigUrl']
desired_capabilities = {}
proxy.add_to_capabilities(desired_capabilities)
URL = "https://mor.nlm.nih.gov/RxClass/search?query=ALIMENTARY%20TRACT%20AND%20METABOLISM%7CATC1-4&searchBy=class&sourceIds=a&drugSources=atc1-4%7Catc%2Cepc%7Cdailymed%2Cmeshpa%7Cmesh%2Cdisease%7Cmedrt%2Cchem%7Cdailymed%2Cmoa%7Cdailymed%2Cpe%7Cdailymed%2Cpk%7Cmedrt%2Ctc%7Cfmtsme%2Cva%7Cva%2Cdispos%7Csnomedct%2Cstruct%7Csnomedct%2Cschedule%7Crxnorm"
service = Service('C:\Program Files\Chrome Driver\chromedriver.exe')
driver = webdriver.Chrome(service=service)
driver.get(URL)
print(driver.requests[0].headers, driver.requests[0].response)
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'tr.dbsearch')))
print(pd.read_html(driver.page_source)[1].iloc[:,:-1])
pd.read_html(driver.page_source)[1].iloc[:,:-1].to_csv('table.csv',index=False)
I'm not sure why I'm receiving an:
TypeError: __init__() got an unexpected keyword argument 'service'
even when I have the path added correctly to my system environment variables as shown below:
Essentially what I'm attempting to do is scrape the data in the table from https://mor.nlm.nih.gov/RxClass/search?query=ALIMENTARY%20TRACT%20AND%20METABOLISM%7CATC1-4&searchBy=class&sourceIds=a&drugSources=atc1-4%7Catc%2Cepc%7Cdailymed%2Cmeshpa%7Cmesh%2Cdisease%7Cmedrt%2Cchem%7Cdailymed%2Cmoa%7Cdailymed%2Cpe%7Cdailymed%2Cpk%7Cmedrt%2Ctc%7Cfmtsme%2Cva%7Cva%2Cdispos%7Csnomedct%2Cstruct%7Csnomedct%2Cschedule%7Crxnorm then store it to a pandas dataframe and pass it to a csv file.
If you are still using Selenium v3.x then you shouldn't use the Service() and in that case the key executable_path is relevant. In that case the lines of code will be:
driver = webdriver.Chrome(executable_path='C:\Program Files\Chrome Driver\chromedriver.exe')
Else, if you are using selenium4 then you have to use Service() and in that case the key executable_path is no more relevant. So you need to change the line of code:
service = Service(executable_path='C:\Program Files\Chrome Driver\chromedriver.exe')
driver = webdriver.Chrome(service=service)
as:
service = Service('C:\Program Files\Chrome Driver\chromedriver.exe')
I've been having this problem also since I switched from pip Jupyter to Anaconda Jupyter. This worked for me:
driver = webdriver.Chrome(ChromeDriverManager().install())
Apparently you don't need the service in the Jupyter package.

selenium.common.exceptions.InvalidArgumentException:

I have been trying to solve this problem for some time.
from selenium import webdriver
driver = webdriver.Chrome(executable_path= r'./chromedriver.exe')
x= driver.get ("https://www.google.com/")
After execution a tab opens with url: "data"
Return this to me:
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir
So far it seems like a duplicate question, but it isn't. Searching for solutions, I found two that didn't work:
1- The first solution I tested is to use the default profile to access the website.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument(r'--user-data-dir=C:\Users\Simone\AppData\Local\Google\Chrome\User Data\Profile 3')
options.add_argument(r'--profile-directory=Profile 3')
driver = webdriver.Chrome(executable_path=r'.\chromedriver.exe', chrome_options=options)
driver.get("https://www.google.com/")
After running the code, it actually opens a new window, but it doesn't access the website.
Returns me the same error.
2 - The second solution tested was to use a test profile to access the website.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument(r"--user-data-dir=C:\Users\Simone\AppData\Local\Google\Chrome\User Data\Profile 2")
options.add_argument(r'--profile-directory=Profile 2')
driver = webdriver.Chrome(executable_path=r'.\chromedriver.exe', chrome_options=options)
driver.get("https://www.google.com/")
After executing the code, a new window opens with the test profile, but it doesn't work.
Returns me the same error:
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir
I would be very grateful if you could help me, I'm just learning python.
Thanks in advance. :)
try the below code and see if it helps as i dont see an issue when i am trying from my side
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
s=Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=s)
x= driver.get ("https://www.google.com/")

Use chrome instead of chrome driver

I'm using Python with Selenium but I need to use it with extension (and probably with cookies). Extension is uploaded from ZIP file and I need to change something in this extension settings after instalation so it will be hard to reupload extension every start of project. Is there aby option to use it like that? I was trying to use profile from normal chrome but it doesn't work for me.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
service = Service('D:\\chromedriver.exe') # your driver path
chrome_options = Options()
chrome_options.add_argument \
(r"--user-data-dir=C:\\Users\\czarn\\AppData\\Local\\Google\\Chrome\\User Data") # your chrome user data directory
chrome_options.add_argument(r'--profile-directory=Member') # the profile with the extensions loaded
window = webdriver.Chrome(service=service, options=chrome_options)
I have this error:
Traceback (most recent call last):
File "C:\Users\czarn\PycharmProjects\trening\main.py", line 16, in <module>
window = webdriver.Chrome(service=service, options=chrome_options)
TypeError: __init__() got an unexpected keyword argument 'service'
As you mentioned, you can use an already existing Chrome Profile.
Example code where the paths are referring to my machine but it should be easy for you to adapt to your use case.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
service = Service('C:\\Users\\nicoc\\PycharmProjects\\WriteAI\\chromedriver.exe') # your driver path
chrome_options = Options()
chrome_options.add_argument \
(r"--user-data-dir=C:\\Users\\nicoc\\AppData\\Local\\Google\\Chrome\\User Data") # your chrome user data directory
chrome_options.add_argument(r'--profile-directory=Default') # the profile with the extensions loaded
driver = webdriver.Chrome(service=service, options=chrome_options)
driver.get("https://google.com/")
The code works for Selenium v4 (it's still in beta but it works fine)

I am getting the exception while running below code

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver=webdriver.chrome(executable_path="C:\Driver\chromedriver_win32\chromedriver.exe")
driver.get("www.youtube.com")
print (driver.title)
driver.close()
for the above code I'm getting the error
TypeError: 'module' object is not callable
I recommend modular programming.
Set a function that returns the chrome driver like such:
# Opens chrome driver
def openChrome():
# directory to chromedrive
chromeDriver = "C:\Driver\chromedriver_win32\chromedriver.exe"
return webdriver.Chrome(chromeDriver)
Call this function like this:
url = "https://stackoverflow.com/"
driver = openChrome()
driver.get(url)
So in the future if your driver isn't working, you don't have to change it on every script you will change it in one place (function)

TypeError: get() missing 1 required positional argument: 'url' error using GeckoDriver and Firefox through Selenium and Python

Executing below code in pycharm.
from selenium import webdriver
browser = webdriver.Firefox
browser.get('https://www.google.com')
Error:
TypeError: get() missing 1 required positional argument: 'url'
How can I solve the error?
Specify the path the chrome driver is located in for example when calling
webdriver.Firefox(‘C://Users/Username/Downloads/‘)
This worked for me:
from selenium import webdriver
driver = webdriver.Chrome("C:\\Users\Rishabh\Downloads\chromedriver_win32\chromedriver.exe")
driver.get('https://web.whatsapp.com/')
Alternate code:
from selenium import webdriver
driver = webdriver.Chrome(executable_path="C:\\Users\Rishabh\Downloads\chromedriver_win32\chromedriver.exe")
driver.get('https://web.whatsapp.com/')
In my case, I got this error for not using parenthesis ().
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('https://www.google.com')
Try with braces while creating Firefox instance. see below example.
from selenium import webdriver
browser = webdriver.Firefox() #focus on () at the end
browser.get('https://www.google.com')
The constructor is driver = webdriver.Firefox(). So in your code block you need to replace driver = webdriver.Firefox with:
driver = webdriver.Firefox()
Additionally, you may need to pass the absolute path of the GeckoDriver binary as follows:
driver = webdriver.Firefox(executable_path=r'C:\path\to\geckodriver.exe')
The issue is caused bacause there are not,()pharentesis, put the pharantesis end to the line
Check this
In Selenium and python
driver = webdriver.Chrome()

Categories