selenium.common.exceptions.InvalidArgumentException: - python

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/")

Related

How does one implement the headless option in the Selenium 4 WebDriver-Manager?

I have but one hurdle to overcome before I can truly call my first bot complete and that is to figure out where to put the options class(?) in order to run ChromeDriverManager in headless mode, and so it stops opening chrome instances! The way the driver is called is:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
options = Options()
options.headless = True
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
Since the old method of calling webdriver by path hasn't been entirely deprecated yet I don't think there have been very many questions pertaining to the new webdriver-manager. I've found only one or two methods that didn't work, like adding ,options=options after .install() or .options somewhere in the mix. In any case, any suggestions would be appreciated.
try this:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
options.add_argument("--headless")
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
I typed out this comment and never finished it, so my apologies. The correct code to run selenium 4 WebDriver-Manager in headless mode is indeed:
options = Options()
options.headless = True #
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),options=options)
#as opposed to what I was trying: #
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install(),options=options))
I imagine that 'options' just needs to be a direct argument of webdriver.Chrome(), so I think this should also work:
driver = webdriver.Chrome(service=Service,options=options(ChromeDriverManager().install()))
Also, I think I have figured out that headless mode makes it easier for websites to mark you as a bot and prompt you to do captchas as after some time of running, because of either captchas or an error in a change of code, my bot couldn't use the search function while headless was true, but performed perfectly with it disabled.

Changing the chrome profile, which Selenium uses, doesn't work

When i run my program with the following code, it opens a new tab, that uses something like a guest account. Also, I am not able to log into anything with this tab.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument(r'user-data-dir=C:\Users\Nick\AppData\Local\Google\Chrome\User Data\Profile 1')
driver = webdriver.Chrome(executable_path=r'C:\ChromeDriver\chromedriver.exe', chrome_options=options)
driver.get("https://www.google.com/")
Same happened to me. The solution was to remove "\Profile 1" from the first line and add a new line for that.
options.add_argument(r'user-data-dir=C:\Users\Nick\AppData\Local\Google\Chrome\User Data')
options.add_argument('profile-directory=Profile 1')
this should work. If not the case, please check these answers: First and Second

open multiple chrome profile with selenium

when I run this code with chrome already open I get this error:
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 need to have multiple profiles open at the same time, what can I do?
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("user-data-dir=C:\\Users\\utent\\AppData\\Local\\Google\\Chrome\\User Data")
options.add_argument("--profile-directory=Profile 3")
driver = webdriver.Chrome(executable_path=r'C:\Development\chromedriver.exe', options=options)
driver.get("https://www.instagram.com")
Let's say you want to open two chrome profiles
You need to instantiate two web drivers with the profile you want to set.
From instantiate I meant, you need to create two chrome web drivers because once the options are set and you have created the driver, you cannot change this later
So,
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = [Options(), Options()]
options[0].add_argument("user-data-dir=C:\\Users\\utent\\AppData\\Local\\Google\\Chrome\\User Data")
options[1].add_argument("user-data-dir=C:\\Users\\utent\\AppData\\Local\\Google\\Chrome\\User Data")
options[0].add_argument("--profile-directory=Profile 3")
options[1].add_argument("--profile-directory=Profile 4") # add another profile path
drivers = [webdriver.Chrome(executable_path=r'C:\Development\chromedriver.exe', options=options[0]), webdriver.Chrome(executable_path=r'C:\Development\chromedriver.exe', options=options[1])]
drivers[0].get("https://instagram.com")
drivers[1].get("https://instagram.com")
The user-data directory get locked by the first instance and the second instance fails with exception
So the solution is to create a Userdata for each profile
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--user-data-dir=C:\\anyPathWhereYouWantToSaveYourUserData\\profile1")
driver = webdriver.Chrome(executable_path=r'C:\Windows\System32\chromedriver.exe', options=options)
driver.get("https://www.instagram.com")
No worries you can access your new profiles now manually if you would like to just by creating shortcuts you just need to add to your target in your shortcut
"C:\Program Files\Google\Chrome\Application\chrome.exe" --user-data-dir="C:\anyPathWhereYouWantToSaveYourUserData\profile1"

Selenium: Follow adfly redirect

Im trying to make a bot that visits my adfly link using the chrome webdriver. Every time I try to use the code below though, the webdriver tells me that there were too many redirects and doesn't follow through. The code below is just being used for testing at the moment:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--proxy-server="+"http://102.129.249.120:8080")
browser = webdriver.Chrome(options=options)
browser.get("http://raboninco.com/18Whc")
Image of error here
Okay so i figured it out. I can use tor with selenium to get access to adfly. Works great btw. Thanks for the help and time guys. If you want to see the code I used, here it is:
from selenium import webdriver
import os
os.popen(r'C:\Users\joldb\Desktop\Tor Browser\Browser\TorBrowser\Tor\tor.exe')
options = webdriver.ChromeOptions()
options.add_argument("--proxy-server="+"socks5://127.0.0.1:9050")
browser = webdriver.Chrome(options=options)
browser.get("http://raboninco.com/18Whc")

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