Edge automation with Selenium - Credential required constantly - python

I want to open e.g. Google page with Edge using Selenium and Python inside company network.
from selenium import webdriver
browser = webdriver.Edge(r"C:/_path_to_driver_/msedgedriver.exe")
browser.get("https://www.google.com")
Edge opens this site and asks me to insert my mail.
After entering mail, it redirects me to this page:
I have to click always manually on OK. Selenium is not able to click on that OK.
Any idea how to perform a click? OR there is way how to store my mail address or certificate to not being always ask for it? This issue occurs only with the Edge controlled by selenium. Default Edge remembers all settings.

If we use selenium webdriver to automate Edge without any arguments, it will create a new profile every time instead of using the existing user data profile. That's why it asks for credential every time.
As a workaround, you can use user-data-dir and profile-directory to use specific profile to launch Edge using Selenium WebDriver. You can use the profile that stores the credential so that it won't ask for credential when you automate Edge. I think in your case it's the default Edge profile.
You need to install the MS Edge Selenium tools using command pip install msedge-selenium-tools selenium==3.141 then refer to the sample code below:
from msedge.selenium_tools import Edge, EdgeOptions
edge_options = EdgeOptions()
edge_options.use_chromium = True
#Here you set the path of the profile ending with User Data not the profile folder
edge_options.add_argument("user-data-dir=C:\\Users\\username\\AppData\\Local\\Microsoft\\Edge\\User Data");
#Here you specify the actual profile folder
edge_options.add_argument("profile-directory=Profile 2");
driver = Edge(options = edge_options, executable_path = r"C:\_path_to_driver_\msedgedriver.exe")
driver.get('https://www.google.com')
Note: Change the paths in the code to your owns.
If you don't know the path of the specific profile, you could check edge://version/ like below:

Related

Admin forced profile login within Selenium using Edge in Python

I'm designing a tool using Selenium with Edge in python. I've got the following code.
from msedge.selenium_tools import Edge, EdgeOptions
edge_options = EdgeOptions()
edge_options.use_chromium = True
edge_options.add_argument("user-data-dir=C:\\Users\\XXX\\AppData\\Local\\Microsoft\\Edge\\User Data");
edge_options.add_argument("profile-directory=Default");
edge_options.binary_location = r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
browser = Edge(EdgeChromiumDriverManager(log_level=30).install(), options=edge_options)
browser.get(url)
I'm succesfully creating a browser, however when there's already a browser opened I get the following error, which can be solved here. Ultimately it is kind of bad practice using Default profile, since the user interferes with the edgedriver.
user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir
There's multiple fixes, however creating a new profile is easiest by changing profiles.
edge_options.add_argument("profile-directory=Profile x");
This is where my trouble begins, the following options are given within the automated browser: Your admin needs you to sign in. To use profile x, please sign in or switch to a different account.
Basically I'm forced to create a second instance of the same account, switching me back to the initial default profile. Which only works if the user is not using Edge while the tool is running, something I cannot guarantee. Is there a workaround given I don't have admin rights?

How to prevent being signed out of your Google account on a Chrome profile opened in Selenium Python

I managed to start Selenium with a custom Chrome Profile that is signed into a Google Account using ChromeOptions by changing the location of the Chrome Profile folder in my directory, renaming it to "Default" and omitting the "Default" in the path in the argument (cf. explanation here).
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument(r"user-data-dir=C:\ChromeProfiles1")
s=Service(r"C:\Users\Administrator\Desktop\chromedriver.exe")
driver = webdriver.Chrome(service=s, options=options)
driver.get(r"https://drive.google.com")
However, as soon as the browser opens, I am signed out of my Google account. It only stays logged in for a split second. Here's a screen capture of it.
Also, I have tried creating new paths to chrome profiles multiple times and only when using it for the first time respectively the icon of the Google account appears for a brief moment. All of the tries after that make it start as "signed out" right away (the way it looks in my recording after the blue icon disappears). Google still seems to realise that the browser is automated; is there a way around this? Can this be fixed by a change to the Chrome profile?

How to run Explorer independently with Python Selenium

I'm trying to use IE using the selenium module in python.
I want to log in with different login IDs in multiple IE windows, but the login information in IE windows is shared with each other, so independent login is not possible.
I am using selenium version is 3.6 and the explorer version is 11.
How can I fix it?
For example, when I log in to Google email in the first explorer window, when the second explorer window is turned on, I am already logged in to the Google email.
I want to log in to the same site with a different ID at the same time.
IE_1 = ID_1
IE_2 = ID_2
IE_3 = ID_3
....
You can achieve separate logins by running the browser in "private" mode.
This is not specific to IE. You can do this in Chrome Firefox, and other browsers as well.
1. IE in private mode
From the docs, you can set "IE Command-Line Options".
Example below is directly from the documentation.
I have NOT tested the below code myself
as I don't have IE in my environment.
from selenium import webdriver
options = webdriver.IeOptions()
options.add_argument('-private')
options.force_create_process_api = True
driver = webdriver.Ie(options=options) # MIGHT WANT TO CHANGE
# Navigate to url
driver.get("http://www.google.com")
driver.quit()
If you don't have the $PATH set for IE, try this:
driver = webdriver.ie(executable_path="<YOUR_IE_PATH>",
options=options)
2. Chrome in private mode
For Chrome, I can confirm it is valid working code. It's a simplified version of what I use myself.
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--incognito")
driver = webdriver.Chrome(
executable_path="<YOUR_CHROME_DRIVER_PATH>",
chrome_options=chrome_options)
driver.get('https://google.com')
For the full list of acceptable "chrome_options", you can check out the reference. This link may not look "official", but it's actually referenced from the Chromedriver website (under "Recognized Capabilities" - "ChromeOptions object" - "args"), so we can safely rely on this doc.

How to login to a website using Python/Selenium?

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
browser = webdriver.Chrome('C:/Users/xyz/Downloads/chromedriver.exe')
# Define all variables required
urlErep = browser.get('http://www.erepublik.com')
xPathToSubmitButton = "//*[#id='login_form']/div[1]/p[3]/button"
urlAlerts = 'https://www.erepublik.com/en/main/messages-alerts/1'
one = 1
xPathToAlerts = "//*[#id='deleteAlertsForm']/table/tbody/tr[%d]/td[3]/p" %one
def logintoerep():
email = browser.find_element_by_id("citizen_email")
password = browser.find_element_by_id("citizen_password")
email.send_keys('myemail')
password.send_keys('mypassword')
browser.find_element_by_xpath(xPathToSubmitButton).click()
logintoerep()
The text above is code I wrote using Selenium to login to erepublik.com.
My main goal is to verify some information on eRepublik.com whenever someone fills a Google Form, and then complete an action based on the Google Form data. I'm trying to login to eRepublik using Selenium, and in each attempt to run the script(which I need to run 24/7, so that whenever the form gets a new response the script is ran) it creates a new window, and after 10-20 times I've logged in to the website it asks for captcha which Selenium can't complete. While in my existing browser window, I'm already logged in so I don't have to worry about Captcha and can just run my code.
How can I bypass this problem? Because I need the script to be able to login every time on its own, but captcha won't allow that. The best solution would be to use Selenium on my existing browser windows, but it doesn't allow that.
Is is possible to copy some settings from my normal browser windows to the Selenium-run browser windows so that every time logs in automatically instead?
I'm open to any suggestions as long as they can get me to verify and complete a few minor actions in the website I've linked.
You can attach your Chrome profile to Selenium tests
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Path") #Path to your chrome profile
browser = webdriver.Chrome(executable_path="C:\\Users\\chromedriver.exe", chrome_options=options)
First off, CAPTCHAs are meant to do exactly that: repel robots/scripts from brute-forcing, or doing repeated actions on certain app features (e.g: login/register flows, send messages, purchase flows, etc.). So you can only go around... never through.
That being said, you can simulate the logged-in state by doing one of the following:
loading the authentication cookies required for the user to be logged in (usually it's only one cookie with a token of some sorts);
loading a custom profile in the browser that already has that user logged in;
use some form of basic auth when navigating to that specific URL (if the web-app has any logic to support this);
Recommended approach: Usually in most companies (at least from my exp), there usually is a specific cookie, or flag that you can set to disable CAPTCHAs for testing purposes. If this is not the case, talk to your PM/DEVs to create such a feature that permits the testing of your web-app.
Don't want to advertise advertise my content, but I think I best tackled this topic HERE. Maybe it can further help.
Hope you solve the problem. Cheers!

Selenium - use session from current Chrome instance

I'm trying to automate some form filling for a web app. Users have to login to the application and then start filling up pages of forms. I have the following Python script using Selenium that can open a window to my application:
from selenium import webdriver
driver = webdriver.Ie("C:\\Python\\Selenium\\Chrome\\chromedriver.exe")
driver.add_cookie()
driver.set_page_load_timeout(30)
driver.get("myurl/formpages")
driver.maximize_window()
driver.quit()
However, when Selenium starts the Chrome window, the user is not logged in. I want to bypass the need to log in every time.
On another Chrome window, I am already logged in as my test user. So whenever I go to the url on my Chrome window, I am already logged in and don't have to log in again.
Is there any way to pass this data into my Selenium script so that it uses the session currently on my existing Chrome instance, therefore not having to log in via the script?
Historically, this is not possible unfortunately (made frustrating by my agreement when I realize the effort it involves and for each browser!).
I've written code before that takes variables out of a CSV for username and password. This is bad because it's in plaintext but you can also hash the information if you like and handle that in your code.
So to recap, there are mediocre solutions, but no native way to handle this in selenium :(
Selenium by default creates a temporary chrome profile each time you start a new instance and deletes that profile once you quit the instance. If you change the chrome options in selenium driver to use a custom chrome profile and and allow that profile to save cookies, you will be able to login without each time typing your login details etc.

Categories