Handling "Authentication Required" alert box with Python 2.7 + Selenium Webdriver - python

I am having an issue with a secure URL:
Opening the URL creates an "Authentication Required" alert box with username and password fields.
I am fairly new to Selenium Webdriver and Python. I am not familiar with handling alerts and am currently manually typing in credentials until I can get this figured out. I have already tried adding my username/password into the URL. This does not work for me.
Could someone please point me in the direction of entering keys into username and password fields in an alertbox?

Could you try using Keys to tab within the alert?
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get('http://www.url.com/')
wait(driver, 5).until(EC.alert_is_present())
alert = driver.switch_to_alert()
alert.send_keys('username')
alert.send_keys(Keys.TAB)
alert.send_keys('password')
alert.accept()

In case of such authentication, you need pass username and password to server while accessing page to avoid authentication window(which is out of reach of selenium)
Suppose the url you're trying to access is: http://example.com
you'll have to access this url with credentials like following:
driver.get('http://username:password#example.com')
where username is your username and password is your password for the site.

Thanks for all of the responses. Unfortunately, none of these solutions worked for me. I suspect it may have something to do with the creation of a new profile every time firefox was opened by webdriver.
My workaround:
I changed the driver from Firefox to IE, after installing the 32bit IE driver(http://selenium-release.storage.googleapis.com/index.html?path=2.44/). This solved my issue by no longer creating the alertbox, and allowing me to continue with my unittest.

I was having similar issues where adding my username/password into the URL did not work for me. This was because Firefox was alerting me with a confirmation box requiring me to confirm that I wanted to log into the site with the username provided. The following solved this issue:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('https://<username>:<password>#<site-needing-auth>.com')
alert = driver.switch_to_alert()
alert.accept()

None of the answer before helped with my situation. The website I am authenticating to uses single sign on which was posing issues when using username:password#website.com.
In the Authentication window two fields needed to be entered both User Name and Password.
To solve this send the user name and password at one time to the alert box.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.alert import Alert
def login(self):
self.browser = webdriver.Firefox()
self.browser.get(r'websitelogin.com')
wait(self.browser, 1).until(expected_conditions.alert_is_present())
# "Switch" to the Alert browser
alert = Alert(self.browser)
# Send the username, TAB then password all in one go using a python f string
alert.send_keys(f'username{Keys.TAB}password')
alert.accept()

I was having the exact same problems as you until I noticed, that I simply forgot to write: 'https' instead of just http. If you add the 's', for me that did it!
So in code maybe you want to try:
driver.get('https://username:password#domain-name.org')

The below Python code can help to load such a URL prompting for authentication within a JavaScript Popup alert, I was also stuck here for a long time. It's because of Chrome driver will not allow such authentication techniques after the update 59 (probably). There are still backdoors via Selenium using the JavaScript engine in the browser to load such URLs.
driver.get("https://www.google.com")
URL = "https://{username}:{password}#www.example.com"
driver.executeScript("window.open('"+URL+"')")

Related

python selenium with chrome - web page detects selenium and doesn't allow login

I am trying to login into the following page using python, selenium and chrome:
https://www.etoro.com/login
You don't need my username and password to recreate this scenario. Use a made up username and password, it will work the same. If I open a 'normal' chrome window (so not using selenium) and try to login with any username/password (assuming it si incorrect) u get an 'invalid username/password' error.
If I try to login using python selenium and chrome and use my username/password i get an error saying 'An error has occured, please try again'.
Web page therefore recognises selenium and doesn't let me in. Is there anything I can do? I want this 'invalid username/password' also when using selenium and chrome, so that I can login with my correct username and password
Thanks in advance!
You cannot login using the normal chromedriver, but luckily you have two options:
Undetected chromedriver
Install it with pip install undetected-chromedriver (documentation), and then run
import undetected_chromedriver as uc
driver = uc.Chrome()
driver.get(url)
# do stuff to log in
Custom user profile
Alternatively, you can use the normal chromedriver and load a user profile where you are already logged in. With this method when you run driver.get(url) you will be already logged in, because it uses the cookies of the user profile. Here you can find a tutorial on how to create a user profile and load it in chromedriver
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\username\\AppData\\Local\\Google\\Chrome\\User Data")
options.add_argument("profile-directory=Profile 2")
driver = webdriver.Chrome(..., options=options)
driver.get(url)
# now you are already logged in

Python Selenium problems with passing login information to user/password form

So, the last 3 hours or so I have tried to get Selenium to work without success. I managed to make it work with requests and Beautifulsoup, but apparently site uses javascript to load data after login so I cannot scrape the data I want after successful login.
Below is the script I am trying to work with.
``
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
driver = webdriver.Chrome("/home/user/Desktop/chromedriver")
username = "FakeUsername"
password = "PasswordFake"
driver.get("https://www.helen.fi/kirjautuminen")
time.sleep(10)
# find username/email field and send the username itself to the input field
# find password input field and insert password as well
driver.find_element(By.XPATH,'//input[#id="username"]').send_Keys(username)
# click login button
``
(Yes, I know its missing password and submit actions, but I can't get it to write anything into username or password input boxes. Also same script seems to be working fine with github's login page, so I really can't understand what I am doing wrong here).
After running the script, chrome opens, site loads fine, but for some reason I get error
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[#id="username"]"}
I have tried with element ID and Name with similar errors, except it said something about "unable to locate element: css selector ..."
If anyone has some advice to give a newbie, it would be awesome. This is starting to give me headache.
I except the script to write username into username input box, but nothing happens.
this occurs because there is an iframe in the page code. It is necessary to switch to the iframe and then search for the element, follow the code with the correction.
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(executable_path= "chromedriver.exe")
username = "FakeUsername"
password = "PasswordFake"
driver.get("https://www.helen.fi/kirjautuminen")
# find username/email field and send the username itself to the input field
# find password input field and insert password as well
iframe = driver.find_element(By.XPATH, '//iframe[#class="login-iframe"]')
driver.switch_to.frame(iframe)
driver.find_element(By.XPATH,'//input[#id="username"]').send_keys(username)
# click login button

AttributeError: 'dict' object has no attribute 'send_keys' Selenium Webdriver

I'm relatively new to coding and python. I'm trying to automate logging into linkedin to send messages to my connections. I'm using selenium webdriver for this process. I haven't been able to log in yet with the automated process because I'm getting
the error: dict object has no attribute send_keys.
I know in this code 'username' is a dictionary type because I checked and the error is telling me it has no attribute 'send_keys', I get what the error message is saying, that the attribute does not exist, but I don't know how to fix it. I'd also like to ask the variable I've created called 'username' can I call that anything? I know calling it username is probably the best, but I'm asking this for my understanding.
The following code is what I have done so far, I know it's not complete but I like to work and fix issues one line at a time.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
import time
s = Service("/usr/local/bin/chromedriver")
driver = webdriver.Chrome(service=s)
driver.get("https://linkedin.com/login")
time.sleep(2)
username = driver.find_element(By.ID,"username")
username.send_keys("my email address goes here")
I'm also attaching an image so it can be seen what part of the LinkedIn page and tags I'm using to try to log in.
Linkedin inspect element code on signing page
I hope I haven't left anything out, I tried to be as descriptive as possible.
Thanks in advance!
This is a bug!
The method webdriver.find_element() is supposed to return an object webdriver.remote.webelement.WebElement and not a dictionary.
Hence, this behaviour is most likely a bug as documented here and not a coding error of yours.
You might be using an old version of chromium in combination with the newer selenium 4.0.
How to fix it
Option A — Software Update.
Make sure you have the latest version installed for your web browser, web driver and selenium.
Option B — Code Patch.
In case you can't update (I had the problem on my RaspberryPi, here I don't have the option to update Chromium since it is no longer supported.):
You have to activate the w3c option for your webdriver.
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options # [!]
s = Service("/usr/local/bin/chromedriver")
opts = Options() # [!]
opts.add_experimental_option('w3c', True) # [!]
driver = webdriver.Chrome(service=s, options=opts) # [!]
driver.get("https://linkedin.com/login")
time.sleep(2)
username = driver.find_element(By.ID,"username")
username.send_keys("my email address goes here")

Python Selenium sign in

So i want to compose a email to my another gmail account in selenium but they ask me to sign in to my google account i dont have any problem in signing in but can i automate that through a computer i tried to do it but i got a error always here is my code.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
PATH = "C:\\edge driver\\msedgedriver.exe"
driver = webdriver.Edge(PATH)
driver.get("https://mail.google.com/mail/u/0/#inbox")
before = driver.find_element_by_id("identifierId")
before.send_keys("arkodeep.ray#gmail.com")
before.send_keys(Keys.RETURN)
# link = driver.find_element_by_link_text("Compose")
So whenever i run this code i get a error by google saying This browser or app may not be secure. Learn more Can anyone help me how to fix that i want to know how to fix that in selenium using a computer bot in msedge. Can someone help?
Here is the error i am getting
There are several ways to resolve this issue:
Use Firefox instead of Chrome
from selenium import webdriver
driver = webdriver.Firefox()
Try to enable less secure apps to access your account.
You can find the option here:
https://myaccount.google.com/lesssecureapps

How to open up browser after login with MechanicalSoup and Python script?

I write a Python script with the help of MechanicalSoup to automate a login task.
The script successfully runs and I can see that get_url() method returns a url which tells login is successful.
At the end, I want to open up a browser (Google Chrome in my case) at the page after login happened but the problem is that I need again enter username and password via Chrome itself!
Is there any solution for this?
Thanks in advance!
you should use selenium
Forexample for login instagram by firefox the python code is
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("https://www.instagram.com/")
#######################################################################################
emailOrPhone=driver.find_element_by_name("emailOrPhone")
fullName = driver.find_element_by_name("fullName")
username=driver.find_element_by_name("username")
password=driver.find_element_by_name("password")
login_btn = driver.find_element_by_xpath('//*[#class="_0mzm- sqdOP L3NKy "]')
#######################################################################################
emailOrPhone.send_keys('somayeteymuri721373#gmail.com')
fullName.send_keys("somayeteymuri")
username.send_keys("yusf4ty72")
password.send_keys("1752s1353")
login_btn.click()
########################################################################################
also you can use this >>
import mechanicalsoup as mch
browser = mch.StatefulBrowser()
## your code here to login to website
browser.launch_browser()

Categories