How to use the same session on Selenium & python - 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

Related

Automate WhatsApp with Python & store WhatsApp login

I am trying to automate WhatsApp. I use selenium but the issue is that I have to scan QRCode every time I launch a chrome instance. I tried by saving the cookies of logged browser and then load the cookies but it didn't work and still show the QRcode to sign in.
I wanted to store WhatsApp login so that I don't have to log in every time I run the script.
Is this something possible with Selenium or if there is another better way?
Add chrome profile , that will allows reusing session data:
chrom_options.add_argument("user-data-dir=C:\\Users\\AppData\\Local\\Google\\Chrome\\User Data")
To get user data drectory:
open : chrome://version in address bar
copy till user data don't need profile part
To store WhatsApp login u have to open chrome in debug mode. following procedure i used in windows and it works.
first add chrome.exe in environment variable. then open chrome in debug mode from cmd. I use following vbscript to open chrome in debug mode. you can just save the code snip as chrome_launch.vbs and use this script to lauch chrome, only first time u would be asked for scan qr code.
Set objStdOut = WScript.StdOut
set osh = createObject("Wscript.shell")
user_profile = """C:\Users\" & osh.ExpandEnvironmentStrings("%USERNAME%") & "\AppData\Local\Google\Chrome\User Data\selenium_chrome_profile"""
osh.run "cmd /c " & "chrome.exe https://web.whatsapp.com/ --remote-debugging-port=9222 --user-data-dir=" & user_profile, 0, True
set osh = Nothing
after login whatsapp web, used following python snip to attach with existing session
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
driver = webdriver.Chrome('chromedriver.exe', options=chrome_options)

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)

How to open Selenium Browser in User mode, not in robotic window

I have written selenium code and it opens Firefox browser, but the browser it opens appears to be similar in behaviour to a private window, i.e., it does not load user login sessions from the normal Firefox browser, neither does it store sessions once closed. I want to open selenium as if it was operated by a human, in the original Firefox, that does not contain the icon:
TIA
To get your User-Profile see:
Firefox User-Profile
Now you can add Options to your WebDriver.
from selenium.webdriver.firefox.options import Options as FirefoxOptions
options = FirefoxOptions()
options.add_argument('--user-data-dir=/path/to/my/profile')
driver = webdriver.Firefox(options=options)
This should load the previous saved User-Profile.

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

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

selenium web driver does not open firefox with custom prefrences

Trying to open firefox with already installed addons using selenium 2 but always opens with default firefox profile having predefined preferences
from selenium import webdriver
driver = webdriver.Firefox()
The above lines of code initiates firefox with default profile.
How to make it initiate with user specified preferences?
You can launch it with a custom profile using something like:
profile = FirefoxProfile("path.to.profile")
driver = webdriver.Firefox(profile)
There is no need to set profile, it could be created automatically, you just need path to addons, e.g.:
string firebugPath = "C:\\Users\\Administrator\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\x7nutq33.default\\extensions\\firebug#software.joehewitt.com.xpi";
FirefoxProfile profile = new FirefoxProfile();
profile.AddExtension(firebugPath);
Driver = new FirefoxDriver(profile);

Categories