I use the following code to open a Chrome window using Selenium:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.youtube.com/")
However, whenever I enter the site, I am logged out. On other sites, I can manually do so, but Google deems it unsafe to log in with Selenium.
I know that Selenium is a test browser, but is it possible to stay logged in with Selenium so that I can access my user data; or is that not the purpose of Selenium, and calling the Chrome subprocess the way to go?
To start with Selenium isn't a browser as such but a bunch of libraries which helps in simulating Browsing Context.
From the question it's not that clear why you are logged out once you login in to the site. But definitely once you are logged in using Selenium driven ChromeDriver initiated google-chrome Browsing Context you must remain logged in and should be able to access your user data.
Related
the thing is I can login to the site only from one device, which is my browser so I can't use selenium, so I have to use something else which opens my browser and copy the text from an element using python.
I think that you may use one of two solutions:
use selenium to open a browser every time, and automate the login process.
If the site allows you to access without logging in when you access with your default browser(such as Stack Overflow for example - you don't need to login every time you open the website from your device), you can use the same browser profile and it should do the same job - login automatically to that website.
references:
what's a browser profile?
how to load a chrome's browser profile with python3 and selenium?
You guys know how google keeps you signed into different websites? how come whenever I use Selenium/Chrome driver and have it go to a website I'm already signed into, it is like im a fresh user and I have to have Selenium go through the work of signing in, which in some cases I cant because some websites block me from signing in with selenium because they know I'm using a bot.
Google keeps you signed into different websites through your Default profile.
Where as Selenium driven ChromeDriver initiated google-chrome Browsing Context is opened everytime with a newly crated temporary profile.
As it stands and I speak under correction, selenium opens a "private" browser tab.
I wanted to know if there was any way to change that so that the selenium tab can benefit from other tabs that are open, i.e. not need to login because you are already logged in on another non-selenium tab
I think the problem is that Selenium loads up the default browser profile. If you use chrome or a chromium based browser, go to chrome://version and you should see a heading called Profile Path copy that and add it to your program with the options method.
Here is a sample:
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=path_to_chrome_profile") #Path to your chrome profile
driver = webdriver.Chrome(executable_path="path_to_chromedriver", chrome_options=options)
Hope it helps. And yes, make sure you are signed into the account you want selenium to access when you check your path.
This question is a extension from How to get cookies from web-browser with Python?
I would like to extract cookies from the same url for 3 accounts logged into chrome. I've already done the code test from the previous url and it returns me only cookies from a single user. I have already researched the library and apparently does not have this functionality.
That is the code i'm using:
import browser_cookie3
from selenium import webdriver
driver = webdriver.Chrome('C:/Users/pedro/Desktop/chromedriver')
cookies = browser_cookie3.chrome(domain_name='my/url')
print('cookies._cookies')
Look into the selenium module. You are able to run Chrome (in selenium named a webdriver) from Python.
If you start the Chrome browser with selenium (and store it into a variable), you have the selenium webdriver and you are able to log in as you would normally do.
Then cookies = driver.get_cookies() leads to cookies being a dictionary, ready to use.
Reference
I need to allow flash content in an automated manner in Python. I tried using Selenium in Python to do so but could not manage. The issue is that browsers stopped supporting settings that always allow flash content. Moreover, the "Allow" button cannot be accessed via Selenium for instance because it is not a part of the website or a setting in Firefox. Does anyone know about a potential workaround?
Here is an image of the Firefox message that I need to somehow access:
"...The Allow button cannot be accessed via Selenium for instance because it is not a part of the website or a setting in Firefox. Does anyone know about a potential workaround?"
I don't know your OS but if it was my problem...
Try to find a "key press" module to send the A key press into Firefox (ie: the Allow shortcut).
Try to send a mouse-click at the coordinates of Allow button.
A good option to try is pyautogui. Once Flash is enabled by such module (clicker or presser) then you can involve Selenium for whatever you needed to do in the enabled Flash.
To allow flash content in an automated manner using Selenium through Python you need to use an instance of FirefoxProfile() and set_preference() method to configure:
dom.ipc.plugins.enabled.libflashplayer.so to true
plugin.state.flash to 2
Code Block:
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_preference("dom.ipc.plugins.enabled.libflashplayer.so","true")
profile.set_preference("plugin.state.flash", 2)
driver = webdriver.Firefox(firefox_profile=profile, executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get('https://www.google.co.in')
print("Page Title is : %s" %driver.title)
driver.quit()