Selenium does not open page with my chrome profile [Python, chrome-webdriver] - python

im trying to make selenium open a chrome session with my profile, the thing is when i add the param "options" in the driver args it open chrome with my profile, but it keeps in the new tab and does not open any page, when i remove "options" it open chrome without my profile, but it open the webpage i really dont know what im doing wrong, i have this code:
options = webdriver.ChromeOptions()
options.add_argument('user-data-dir=C:\\Users\\---\\AppData\\Local\\Google\\Chrome\\User Data\\')
options.add_argument('-disable-extensions')
options.add_argument('start-maximized')
#this is the problematic line here
driver = webdriver.Chrome(executable_path='C:\chromedriver\chromedriver.exe', chrome_options=options)
driver.get('https://google.com/')
i think i need to add argument for open the webpage in options, but i cant find how if thats the case, thank you

Related

How to use the same session on Selenium & python

Im using Selenium to scrape some data from a website I signed up to, now every time I run the program it opens a new chrome browser and login to my account and eventually I runed into Captcha, how can I make it that it will open the same browser session with my account already logged in?
right now this is what I use:
PATH ="C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("website example")
Thanks!
Create a Guest user profile in your chrome driver.
Add this parameter to your chrome driver instance:
user-data-dir={UserProfilePath}
For me UserProfilePath is - C:\\Users\\My_Username\\AppData\\Local\\Google\\Chrome\\User Data\\Guest Profile

Python Selenium remain logged in a website

I'm running a simple scrape code to scrape a few lines from a website. The problem is that Python always opens a new Chrome window and logs in again every time I run the bot.
So, I read online and created a Chrome profile. Now it opens the Chrome profile, but it still asks me to log in to the website. I guess I need to save cookies. I tried some YouTube tutorials, but I couldn't figure out how to save the cookies. I'm a complete noob, so can anyone explain me how to do so?
This is my code:
options = Options()
options.add_argument("user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 2")
driver = webdriver.Chrome(executable_path=r'C:\Program Files (x86)\chromedriver.exe', chrome_options=options)
driver.get("https://websitetologin.com")
search = driver.find_element_by_name("fm-login-id")
search.send_keys("loginemail")
search.send_keys(Keys.RETURN)
time.sleep(3)
search = driver.find_element_by_name("fm-login-password")
search.send_keys("loginpassword")
search.send_keys(Keys.RETURN)
time.sleep(3)
search = driver.find_element_by_class_name("fm-button")
search.send_keys(Keys.RETURN)
time.sleep(3)
You can use the chrome options as well user-data-dir=selenium
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=selenium")
driver = webdriver.Chrome(options =options)
it would save cookies for current session, that can be used later for profiles and folders.
You can refer here for more
or
driver.get('http://google.com')
for cookie in cookies:
driver.add_cookie(cookie)

Python Selenium Chrome "user data directory is already in use"

I'm trying to make a script in python with selenium that will open a website and click a button.
Problem is that I have to close chrome before I can run the script otherwise 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
Is there a way around this so I don't have to close chrome before running the script everytime?
My code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
PATH = "C:\Program Files (x86)\chromedriver.exe"
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:/Users/username/AppData/Local/Google/Chrome/User Data")
driver = webdriver.Chrome(executable_path=r'C:\Program Files (x86)\chromedriver.exe', chrome_options=options)
driver.get("https://disboard.org/sv/dashboard/servers")
link = driver.find_element_by_link_text("bump")
link.click()
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
...implies that the user data directory Default is already in use so ChromeDriver was unable to access the directory and initiate/spawn a new Browsing Context i.e. Chrome Browser session.
Solution
In such cases, you can add/create a new Chrome Profile following the steps below and use it for the AUT (Application Under Test):
Open Google Chrome, and select the user icon in the top right and click on Add.
Enter a name for the new person, select an icon to help represent this new account and make it easier to find later. Then, select Add.
You will find the Chrome Profile shortcut created on the Desktop.
Additionally, you will find a new sub-directory Profile 1 being created beside Default
Now, you can use the Profile 1 sub-directory as follows:
options = Options()
options.add_argument("start-maximized")
options.add_argument("--profile-directory=Profile 1")
options.add_argument("--user-data-dir=C:/Users/user/AppData/Local/Google/Chrome/User Data")
driver = webdriver.Chrome(executable_path=r'C:\BrowserDrivers\chromedriver.exe', options=options)
driver.get("https://www.google.com/")
References
You can find a couple of relevant detailed discussions in:
How to use Chrome Profile in Selenium Webdriver Python 3
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: user data directory is already in use error with real Chrome Browser
Selenium: Point towards default Chrome session
If it is crucial for your use case that you can use both your Chrome profile and your automation with your profile at the same time, here's a possible solution:
Install a second (older or newer) version of Chrome as described in this thread
Enable synchronization on the desired profile in your current Chrome.
Log in with the desired profile from the second version of Chrome and enable synchronization there as well.
Use the chrome driver compatible with the second version of Chrome in your code.
There may be some problems with synchronization not loading everything that you need, but if it's something simple like saved passwords then this should work.
The reason for your issue is because you have added the following argument:
options.add_argument("user-data-dir=C:/Users/username/AppData/Local/Google/Chrome/User Data")
The argument is instructing selenium to use your local chrome profile. Hence, as you have a chrome session open, the automation cannot execute until you close down the browser session.
Remove the argument to execute an independent automation test profile.
See below for the remediated code provided from your question
from selenium import webdriver
PATH = "C:\Program Files (x86)\chromedriver.exe"
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(executable_path=PATH, options=options)
driver.get("https://disboard.org/sv/dashboard/servers")
link = driver.find_element_by_link_text("bump")
link.click()

How to open a browser tab after GET and POST requests in python

Im not sure if this is possible, but basically, I'm trying to open a tab in Chrome to show the outcome of my GET and POST requests. For instance, lets say im using Python requests to POST log in data to a site. I would want to then open a chrome tab after that POST request to see if I did it correctly, and also to continue on that webpage from there in Chrome itself. I know I can use response.text to check if my POST request succeeded, but is there a way I can physically open a chrome tab to see the result itself? Im guessing there would be a need to export some sort of cookies as well? Any help or insights would be appreciated.
When using selenium, you need to remove headless option for browser:
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
#chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
wd = webdriver.Chrome('<PATH_TO_CHROMEDRIVER>', options=chrome_options)
# load page via selenium
wd.get("<YOUR_TARGET_URL>")
# don't close browser
# wd.quit()
Also remove closing browser in the end of code.
After that browser will remain open and you will be able to continue work manually.

Python Selenium - URL won't open in browser instance

This particular URL won't open via python selenium script below. This same code works for most of the urls I have tried it on.
chrome_driver = 'chromedriver.exe' # Change this to your chrome driver path
driver = webdriver.Chrome(chrome_driver)
driver.get(url)
There is no any error message displayed, it will only show blank page and nothing will happen. When manually typed into regular chrome browser, it opens correctly but when manually into chrome browser instance, it won't work just like the script.
What is the solution to fix this?

Categories