I'm using Selenium and ChromeDriver to scrape data from a website.
I need to keep my account logged in after closing the Driver: for this purpose I use every time the default Chrome profile.
Here you can see my code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
urlpage = 'https://example.com/'
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\MyName\\AppData\\Local\\Google\\Chrome\\User Data")
driver = webdriver.Chrome(options=options)
driver.get(urlpage)
The problem is that for some websites (e.g. https://projecteuler.net/) it works, so I'm logged in also the following session, but for other (like https://www.fundraiso.ch, the one I need) it doesn't, although in the "normal" browser I'm still logged in after I close the window.
Does anyone know how to fix this problem?
EDIT:
I didn't mention that I can't automate the login because the website has a maximum login number, and if I breach it the website will block my account.
Related
I am automating a form-filler using selenium, however the issue is the user needs to be logged in to their google account. Selenium is opening up a new browser instance where the user is not logged in. I cannot automate the log in process due to 2 factor authentication.
So far I've found
import webbrowser
webbrowser.open('www.google.com', new = 2)
which will open the window the way I want with the user logged in, however I am unable to interact with the page unlike selenium. Is there a way I can get selenium to open a window like webbrowser? Or is there a way to interact with the page with webbrowser? I have checked the docs of both and not seen an answer to this
You don't need to make the user log in again to their user account. You can use the same chrome profile that you have for your browser. This will enabled you to use all your accounts from chrome without making them log explicitly.
Here is how you can do this :
First get the user chrome profile path
Mine was : C:\Users\hpoddar\AppData\Local\Google\Chrome\User Data
If you have multiple profiles you might need to get the Profile id for your chrome google account as well.
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium import webdriver
chrome_path = r"C:\Users\hpoddar\Desktop\Tools\chromedriver_win32\chromedriver.exe"
options = webdriver.ChromeOptions()
options.add_argument(r"--user-data-dir=C:\Users\hpoddar\AppData\Local\Google\Chrome\User Data")
# Specify this if multiple profiles in chrome
# options.add_argument('--profile-directory=Profile 1')
s = Service(chrome_path)
driver = webdriver.Chrome(service=s, options=options)
driver.get("https://www.google.co.in")
I'm trying to use Selenium (3.141.0) with ChromeDriver (87.0.4280) to access a page. When accessed manually, it brings me to a policy page (different URL) where you have to hit 'Ok' before continuing to the site. Edit This is using Win 10 and I have the folder with the chromedriver on PATH.
When using the following code, I'm able to get to the policy page with the ("--headless") option but without it I get a blank page with 'data:,' in the URL and nothing else loads. I've tried accessing straight from the policy page and the site URL but they both get stuck when the webdriver is created. Am I missing something? I'm open to any suggestions, thanks!
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--headless")
driver_path = 'D:\....\chromedriver.exe'
driver = webdriver.Chrome(executable_path= driver_path, options= chrome_options)
driver.get(...) # left out the url
This is the output page I get without using ("--headless")
Funny enough, I realized it was because my Chrome Developer tools had become disabled. Not sure how but when I re-enabled them, it worked perfectly again. Weird.
I am trying to scrape a site that is behind authentication. When I use firefox, I am always already logged on the site (even after a reboot) and I can go straight into the pages.
When I open selenium and tell it to use my firefox profile, the selenium browser isn't logged in. Then I have to go through about 4 or 5 min of clicking pictures to show that I'm a person. But if I open firefox myself immediately afterwards, I am still logged in - no problem.
So I really don't understand what is happening. I know that my firefox profile is being loaded: When I initialize the browser, it takes around 60 seconds for the profile to be copied over before the browser opens up. My code is below:
from selenium.webdriver import Firefox
from selenium import webdriver
url_start = 'https://authenticatedsection.some site.com/'
fp = webdriver.FirefoxProfile('C:/Users/Claudia/AppData/Roaming/Mozilla/Firefox/Profile/minvococ.default-release')
browser = Firefox(fp)
#initialize browser
browser.get(url_start)
You're logged in because of the cookies stored by your firefox browser. Selenium opens a new browser process with no cookies. If you only need to enter a user name and password to login, this can be done with selenium. If you need to pass a "human-test" (captcha) such as the picture clicking task you're describing, this cannot be done via scraper: it is specifically designed to prevent bots from logging onto that page.
Why do you want your scraper to log into your firefox profile?
I'm working on trying to automate a game I want to get ahead in called pokemon vortex and when I login using selenium it works just fine, however when I attempt to load a page that requires a user to be logged in I am sent right back to the login page (I have tried it outside of selenium with the same browser, chrome).
This is what I have
import time
from selenium import webdriver
from random import randint
driver = webdriver.Chrome(r'C:\Program Files (x86)\SeleniumDrivers\chromedriver.exe')
driver.get('https://zeta.pokemon-vortex.com/dashboard/');
time.sleep(5) # Let the user actually see something!
usernameLoc = driver.find_element_by_id('myusername')
passwordLoc = driver.find_element_by_id('mypassword')
usernameLoc.send_keys('mypassword')
passwordLoc.send_keys('12345')
submitButton = driver.find_element_by_id('submit')
submitButton.submit()
time.sleep(3)
driver.get('https://zeta.pokemon-vortex.com/map/10')
time.sleep(10)
I'm using python 3.6+ and I literally just installed selenium today so it's up to date, how do I force selenium to hold onto cookies?
Using a pre-defined user profile might solve your problem. This way your cache will be saved and will not be deleted.
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--user-data-dir=C:/Users/user_name/AppData/Local/Google/Chrome/User Data")
driver = webdriver.Chrome(options=options)
driver.get("xyz.com")
I am automating certain tasks using Web Browser with Selenium. Suppose I open a webpage say Facebook or Quora using webdriver, the page that opened asks for the username and password again even though I am still logged in.
from selenium import webdriver
b = webdriver.Chrome()
b.get("https://www.quora.com/")
I want the webdriver to use and retain the information of the current session so that I am able to land on my profile without having to enter my username and password again. How can I achieve this? Thanks.
Edit 1 : I tried pointing it to the chrome user data, but isn't working.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_option = Options()
chrome_option.add_argument('user-data-dir=~/Library/Application Support/Google/Chrome/Default')
b = webdriver.Chrome(executable_path="/Users/mymac/Downloads/chromedriver",chrome_options=chrome_option)
b.get("https://www.quora.com/")