i'm trying to access a pop-up authentication box in a internal link which i can t share as it is confidential, i tried the below code to access it, it works fine for a single link but i have multiple links which i have it under a loop.
driver.get(links[i])
time.sleep(2)
window_before = driver.window_handles[0]
driver.switch_to.window(window_before)
shell = win32.Dispatch("WScript.Shell")
time.sleep(2)
shell.Sendkeys('username')
shell.Sendkeys('{TAB}')
shell.Sendkeys('password')
shell.Sendkeys('{ENTER}')
time.sleep(4)
#Second login page Enter password again and log in
password = driver.find_element_by_name('p_t02').send_keys('password')
logon = driver.find_element_by_xpath("//td[#class='t10C']").click()
Problem: when it runs the second loop for the second link it actually skips to the below part and throws an error
driver.get(links[i])
time.sleep(2)
window_before = driver.window_handles[0]
driver.switch_to.window(window_before)
shell = win32.Dispatch("WScript.Shell")
time.sleep(2)
shell.Sendkeys('username')
shell.Sendkeys('{TAB}')
shell.Sendkeys('password')
shell.Sendkeys('{ENTER}')
time.sleep(4)
it skips the above and goes to the below code and throws an error
#Second login page Enter password again and log in
password = driver.find_element_by_name('p_t02').send_keys('password')
can anyone help me get past the authentication with another way
like without using the
shell = win32.Dispatch("WScript.Shell")
Related
I am trying to automate Linked in using selenium.
I want to search Journalist and wants to connect them all.
So, I wrote the code that click all the connect buttons.
but it is not working.
from selenium import webdriver
import time
driver = webdriver.Chrome('C:\\Users\\SUJASH\\AppData\\Local\\Temp\\HZ$D.705.3782\\HZ$D.705.3783\\chromedriver.exe')
driver.get('https://www.linkedin.com')
time.sleep(2)
#********** LOG IN *************
username = driver.find_element("id", "session_key")
password = driver.find_element("id", "session_password")
#above code will find the box of Username and Password
username.send_keys('HIDDEN_ON_SOF')
password.send_keys('HIDDEN_ON_SOF')
time.sleep(2)
#above code will enter the username and Password
submit = driver.find_element("xpath", "//button[#type='submit']").click()
#above code will click the submit button
#*********** SEARCH ***********
driver.get('https://www.linkedin.com/search/results/people/?heroEntityKey=urn%3Ali%3Aautocomplete%3A1975299068&keywords=entertainment%20journalist&origin=CLUSTER_EXPANSION&sid=uv!')
time.sleep(5)
'''above code has link of my search that I want to do, I want to search Entertainment journalist, so I searched Entertainmnet journalist and copy the link and pasted it here'''
#************ from here script is not working ****************************
all_buttons = driver.find_elements("tag_name", "button")
connect_buttons = [btn for btn in all_buttons if btn.text == "Connect"]
for btn in connect_buttons:
driver.execute_script("arguments[0].click();", btn)
time.sleep(2)
send = driver.find_element("xpath" , "//button[#aria-label='Send now']")
driver.execute_script("arguments[0].click();", send)
close = driver.find_element("xpath" , "//button[#aria-label='Dismiss']")
driver.execute_script("arguments[0].click();", close)
time.sleep(2)
''' Above code is not working, I want to click on all the connect buttons but code is not working after the comment *** from here script is not working *** '''
Am using selenium and python plus 2capture API.i was able to retrieve the tokens successfully and even submit the form using js.
The form is submitted but the link keeps on reloading therefore cannot go past the hcapture loop.
here is my code:
def Solver(self, browser):
WebDriverWait(browser, 60).until(Ec.frame_to_be_available_and_switch_to_it((By.XPATH,'//*[#id="cf-hcaptcha-container"]/div[2]/iframe')))
captcha = CaptchaRecaptcha()
url = browser.current_url
code = captcha.HCaptcha(url)
script = "let submitToken = (token) => {document.querySelector('[name=h-captcha-response]').innerText = token document.querySelector('.challenge-form').submit() }submitToken('{}')".format(code)
script1 = (f"document.getElementsByName('h-captcha-response')[0].innerText='{code}'")
print(script)
browser.execute_script(script)
time.sleep(5)
browser.switch_to.parent_frame()
time.sleep(10)
Am using proxies in the web driver and also switching the user agent
someone, please explain what am doing wrong or what I should do to break the loop.
Help me figure out why when using the get () method a second time, the page does not go? The method works only if you use a time delay time.sleep ()
Not working:
LOGIN = 'something#mail.com'
PASS = 'somepass'
LINK = 'https://stepik.org/'
browser = webdriver.Chrome()
browser.get(LINK)
browser.implicitly_wait(5)
browser.find_element_by_id('ember232').click()
username = browser.find_element_by_name('login').send_keys(LOGIN)
pas = browser.find_element_by_name('password').send_keys(PASS)
button = browser.find_element_by_xpath('//button[#type = "submit"]').click()
browser.get('https://stepik.org/lesson/237240/step/3?unit=209628')
Working
LOGIN = 'something#mail.com'
PASS = 'somepass'
LINK = 'https://stepik.org/'
browser = webdriver.Chrome()
browser.get(LINK)
browser.implicitly_wait(5)
browser.find_element_by_id('ember232').click()
username = browser.find_element_by_name('login').send_keys(LOGIN)
pas = browser.find_element_by_name('password').send_keys(PASS)
button = browser.find_element_by_xpath('//button[#type = "submit"]').click()
time.sleep(5)
browser.get('https://stepik.org/lesson/237240/step/3?unit=209628')
You are trying to login into the web site and then to navigate to some internal page.
By clicking the submit button
button = browser.find_element_by_xpath('//button[#type = "submit"]').click()
You are trying to log into the site.
This process takes some time.
So if immediately after clicking the submit page, while login is still not proceed, you are trying to navigate to some internal page this will not work since you still not logged in.
However you do not need to use a hardcoded sleep of 5 seconds.
You can use an explicit wait of expected conditions like presence_of_element_located() of some internal element to indicate you are inside the web site. Once this condition is fulfilled you can navigate to the desired internal page.
Try an alternative way:
driver.navigate().to("https://stepik.org/lesson/237240/step/3?unit=209628")
I know that multiple people have asked a similar question about this however, I would like to know how to login to gmail (or google account) using python. I have a code already (see below) that can loggin the user to gmail using selenium. However I noticed a few problems.
The browser closes when the program stops/closes.
It can not detect a failed login.
Both problems really need to be solved for me to be able to work on my project. I don't mind using something else than selenium like pyautogui to open google. However, it needs to be able to detect a failed login and then close the browser, if the login is successful the browser should stay open for the user to use.
My code:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
def gmail_login(username, password):
gmailId = username
passWord = password
driver = webdriver.Chrome(ChromeDriverManager().install())
try:
driver.get(r'https://accounts.google.com/signin/v2/identifier?continue=' + \
'https%3A%2F%2Fmail.google.com%2Fmail%2F&service=mail&sacu=1&rip=1' + \
'&flowName=GlifWebSignIn&flowEntry = ServiceLogin')
driver.implicitly_wait(3)
loginBox = driver.find_element_by_xpath('//*[#id ="identifierId"]')
loginBox.send_keys(gmailId)
nextButton = driver.find_elements_by_xpath('//*[#id ="identifierNext"]')
nextButton[0].click()
passWordBox = driver.find_element_by_xpath(
'//*[#id ="password"]/div[1]/div / div[1]/input')
passWordBox.send_keys(passWord)
nextButton = driver.find_elements_by_xpath('//*[#id ="passwordNext"]')
nextButton[0].click()
except:
driver.close()
gmail_login("email#gmail.com", "Password")
I tought of checking the url after the program has finished if it is equal to a logged in url however that didn't really work too well and now I am out of ideas.
Updated 02-14-2021
I was able to extract this error message:
using this code:
signin_failure = driver.find_elements_by_xpath("//*[contains(text(),'Couldn’t sign you in')]")
if signin_failure[1].text == 'Couldn’t sign you in':
print('something went wrong')
In a normal gmail login you will get one of these two error messages:
Couldn’t find your Google Account
Wrong password. Try again or click Forgot password to reset it
The XPATH to get these error messages is:
wrong_email = driver.find_elements_by_xpath("//*[contains(text(),'Couldn’t find your Google Account')]")
wrong_password = driver.find_elements_by_xpath("//*[contains(text(),'Wrong password. Try again or click Forgot password to reset it')]")
if you want to close the browser after an error message, such as Couldn’t sign you in then add a driver.close() statement.
signin_failure = driver.find_elements_by_xpath("//*[contains(text(),'Couldn’t sign you in')]")
if signin_failure[1].text == 'Couldn’t sign you in':
print('something went wrong')
driver.close()
If you want to keep the browser open then don't use the driver.close() statement, but add this experimental_option
chrome_options.add_experimental_option("detach", True)
I was also able to throw these error messages:
signin_failure = driver.find_elements_by_xpath("//*[contains(text(),'Couldn’t sign you in')]")
# this message was throw when the next button was clicked prior to entering a username
no_input = driver.find_elements_by_xpath("//*[contains(text(),'Enter a valid email of phone number')]")
PSEUDOCODE CODE:
This is how you could do it, but you might have to adjust the code as you test.
def gmail_login(username, password):
driver = webdriver.Chrome(ChromeDriverManager().install())
try:
driver.get(r'https://accounts.google.com/signin/v2/identifier?continue=' + \
'https%3A%2F%2Fmail.google.com%2Fmail%2F&service=mail&sacu=1&rip=1' + \
'&flowName=GlifWebSignIn&flowEntry = ServiceLogin')
driver.implicitly_wait(3)
loginBox = driver.find_element_by_xpath('//*[#id ="identifierId"]')
loginBox.send_keys(username)
nextButton = driver.find_elements_by_xpath('//*[#id ="identifierNext"]')
nextButton[0].click()
wrong_email = driver.find_elements_by_xpath("//*[contains(text(),'Couldn’t find your Google Account')]")
# you need to check this slice
if wrong_email[1].text == 'Couldn’t find your Google Account':
print('something went wrong')
driver.close()
else:
passWordBox = driver.find_element_by_xpath('//*[#id ="password"]/div[1]/div / div[1]/input')
passWordBox.send_keys(password)
nextButton = driver.find_elements_by_xpath('//*[#id ="passwordNext"]')
nextButton[0].click()
wrong_password = driver.find_elements_by_xpath("//*[contains(text(),'Wrong password. Try again or click Forgot password to reset it')]")
# you need to check this slice
if wrong_password[1].text == 'Wrong password. Try again or click Forgot password to reset it':
print('something went wrong')
driver.close()
except:
driver.close()
Original post
Google forbids using automated scripts for logging into Gmail. I have tried using selenium and I get this warning.
When I click Learn More I get this message.
Please note this line: Are being controlled through software automation rather than a human
Here are other question where they discuss work arounds for this Google direct login issue:
“This browser or app may not be secure” error while attempting to login in to Gmail account using Selenium
GMail is blocking login via Automation (Selenium)
You should be looking at the Gmail API for programmatic access, it will work a lot better than trying to drive the UI like selenium does.
https://developers.google.com/gmail/api/quickstart/python
I have no time right now to code, but:
To make your browser stay open just delete your driver.close() function, this is what makes it to stop when program reach it.
To solve your problem, just try to make a successfull login and a failing one, look for a WebElement in both that indicates one of the events in an unique way, then, put the driver.close() in an if declaration only if selenium finds the WebElement in viewport after the login (failed one), otherwise do not let it execute the instruction.
Easy as that.
I am trying to log in to my amboss account using Selenium webdriver with python, but as I dont have much experience with it I dont understand what goes wrong. My credentials (email and password) are correct as I have used them to log into the website before.
Here is my code so far:
# run firefox webdriver from executable path
driver = webdriver.Firefox(firefox_options=options, capabilities=cap, executable_path = path_to_driver)
driver.get("https://www.amboss.com/us/account/login")
signinusername = config['amboss']['email']
signinpassword= config['amboss']['password']
username = driver.find_element_by_id("signin_username")
username.clear()
username.send_keys(signinusername)
pwd = driver.find_element_by_xpath("//*[#id='signin_username']")
pwd.clear()
pwd.send_keys(signinpassword)
loginbutton = driver.find_element_by_xpath("/html/body/div[2]/div[1]/div/div/form/div[4]/input").click()
time.sleep(20)
# execute script to scroll down the page
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;")
#element in log in page
newelement = driver.find_element_by_xpath("//*[#id='left']/p[1]/strong")
print(newelement.get_attribute('innerHTML'))
What I try to do here is log in to the platform and then grab an element which I see in the welcome page by xpath. Despite that selenium is unable to find this element and I get the following error:
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //*[#id='left']/p[1]/strong
Does anyone understand why this happens? Is it because log in was not successful or could something else be wrong? Thanks in advance
UPDATE
The elements you were trying to fetch were in an iframe so you need to switch to that iframe so they be visible. The following works, try it.
username = driver.find_element_by_id("signin_username")
username.clear()
username.send_keys(signinusername)
pwd = driver.find_element_by_id('signin_password')
pwd.clear()
pwd.send_keys(signinpassword)
loginbutton = driver.find_element_by_class_name('amboss-button').click()
time.sleep(2)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;")
time.sleep(2)
frames = driver.find_elements_by_tag_name('iframe')
driver.switch_to.frame(frames[4])
newelement = driver.find_elements_by_tag_name('p')[0].text # This prints the following 'You now have access to AMBOSS—an all-in-one pl....'
print(newelement)
I'm assuming you're trying to login to the system and trying to find something.
First of all your login attempt is not correct. You're entering password in the username field in your code.
Please do login like this:
At first, add this import to your python script:
from selenium.webdriver.common.keys import Keys
Then do login like this:
username = driver.find_element_by_id("signin_username")
username.clear()
username.send_keys(signinusername)
pwd = driver.find_element_by_id("signin_password")
pwd.clear()
pwd.send_keys(signinpassword)
pwd.send_keys(Keys.ENTER)
I'm not logging in by clicking the button. Just hitting an ENTER after logging in.
I guess correcting the login attempt will help you to find your element and print contents inside this.
I've created an account in amboss to answer your question and after logging in the only iframe I found was from google tag manager with no contents in it. So I'm not really sure if your account contains some other web views.
Hope correcting the login attempt helps.
First you wrong this is line :
pwd = driver.find_element_by_xpath("//*[#id='signin_username']")
Please change with :
pwd = driver.find_element_by_id('signin_password')
Second, use conditional if for validation success or failed login :
click login here
time.sleep(20)#here recommendation use WebDriverWait
count = len(driver.find_elements_by_xpath("//*[#id='left']/p[1]/strong"))
if count > 0:
print("login success")
print(driver.find_element_by_xpath("//*[#id='left']/p[1]/strong").text)
else:
print("login failed")
Make sure this is the correct locator : //*[#id='left']/p[1]/strong