Is it possible to validate when an entered password is wrong? - python-3.x

When writing an auto-it for safari with selenium, is it possible to know if a password inserted into a website login is wrong?
I've tried using try: and except:, but selenium doesn't raise an error when the password is wrong.
an example would be:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver=webdriver.Safari()
driver.get(r'website')
usr=driver.find_element_by_name(r'username')
usr.clear()
usr.send_keys(r'username')
pas=driver.find_element_by_name(r'password')
pas.clear()
pas.send_keys(r'password')
pas.send_keys(Keys.RETURN)

Use WebDriverWait and element_to_be_clickable and following css selector.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver=webdriver.Chrome()
driver.get(r'https://auth.florimont.ch/cas/login;jsessionid=8B5A3B65779756F3CF71ACB41E578AC3?service=https%3A%2F%2Fflore.florimont.ch%2Fhome')
usr=driver.find_element_by_name(r'username')
usr.clear()
usr.send_keys(r'username')
pas=driver.find_element_by_name(r'password')
pas.clear()
pas.send_keys(r'password')
pas.send_keys(Keys.RETURN)
print(WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#fm1 #msg"))).text)
Print on console:
Invalid credentials.
Browser snapshot:

To validate if a username/password inserted into a website login is wrong you can use a try/catch{} block and you can use the following Locator Strategies:
Code Block:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("start-maximized")
driver = webdriver.Chrome(options=chrome_options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("https://auth.florimont.ch/cas/login?service=https%3A%2F%2Fflore.florimont.ch%2Fhome")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#username"))).send_keys("a_random_programmer")
driver.find_element_by_css_selector("input#password").send_keys("a_random_programmer")
driver.find_element_by_css_selector("input.btn-submit").click()
try:
WebDriverWait(driver, 20).until(EC.text_to_be_present_in_element((By.XPATH, "//h2[text()='Enter your Username and Password']//preceding::div[1]"), "Invalid credentials."))
print("Invalid Login")
except TimeoutException as e:
print("Valid Login")
driver.quit()
Console Output:
Invalid Login

Related

selenium - Not able to find input

Trying to create script which will subscribe to news automatically, but stuck with a problem, selenium not able to find email input and submit button. Everytime getting selenium.common.exceptions.NoSuchElementException:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chrome_options = Options()
chrome_options.add_argument("--window-size=1920x1080")
path_to_chromedriver = 'chromedriver'
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=path_to_chromedriver)
driver.get('https://dataengweekly.com/')
driver.find_element_by_tag_name("body").send_keys(Keys.PAGE_DOWN)
email_input = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, 'input[type="email"]'))
)
email_input.send_keys("email#test.com")
driver.find_element_by_css_selector('button.subscribe-btn').click()
time.sleep(10)
Note - Your subscription textbox is in a different iframe, To work with that iframe you need to first switch to that iframe.
Try below code and let me know if you need more clarification -
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
import time
chrome_options = Options()
chrome_options.add_argument("--window-size=1920x1080")
driver = webdriver.Chrome(options=chrome_options)
wait = WebDriverWait(driver, 5)
action = ActionChains(driver)
driver.get('https://dataengweekly.com/')
iframe = driver.find_element_by_xpath('//iframe')
driver.switch_to.frame(iframe)
email_input = wait.until(EC.presence_of_element_located((By.XPATH, "//input[#type='email']")))
action.move_to_element(email_input).click().send_keys("email#test.com").perform()
driver.find_element_by_css_selector('button.subscribe-btn').click()
time.sleep(2)

python: Selenium saves empty file

I am trying to download a file from the following link by clicking the download button: https://www.investing.com/equities/oil---gas-dev-historical-data
Here is my code:
from datetime import date
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import ElementClickInterceptedException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.preferences.instantApply",True)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/csv")
profile.set_preference("browser.helperApps.alwaysAsk.force",False)
profile.set_preference("browser.download.manager.showWhenStarting",False)
profile.set_preference("browser.download.folderList",0)
driver = webdriver.Firefox(firefox_profile=profile)
driver.get('https://www.investing.com/equities/oil---gas-dev-historical-data')
try:
popup = WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "i[class*='largeBannerCloser']")))
popup.click()
except TimeoutException as to:
print(to)
#driver.find_element_by_css_selector("i.popupCloseIcon").click()
driver.find_element_by_css_selector("a[class*='login']").click()
driver.find_element_by_id('loginFormUser_email').send_keys('myemail')
driver.find_element_by_id('loginForm_password').send_keys('pass')
driver.find_element_by_xpath("//div[#id='loginEmailSigning']//following-sibling::a[#class='newButton orange']").click()
driver.find_element_by_id('flatDatePickerCanvasHol').click()
start_date = driver.find_element_by_id('startDate')
start_date.send_keys(Keys.BACKSPACE*10)
start_date.send_keys(date(2014,1,1).strftime("%d/%m/%Y"))
driver.find_element_by_id('applyBtn').click()
#driver.implicitly_wait(30)
driver.find_element_by_css_selector('a.newBtn.LightGray.downloadBlueIcon.js-download-data').click()
But it always saves empty file? How can I avoid this behavior?
The page seems to export data displayed on the page only. adding a check to just wait for the table to load.
After changing the date range helped in downloading the data successfully.
try:
popup = WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.XPATH, "//div[#id='results_box']//tbody//tr")))
except TimeoutException as to:
print(to)
driver.find_element_by_css_selector('a.newBtn.LightGray.downloadBlueIcon.js-download-data').click()

Issue with login using selenium and python

I am getting following error
no such element: Unable to locate element: {"method":"xpath","selector":"//input[#placeholder='User ID']"}
(Session info: chrome=78.0.3904.70). Let me know how can i pass user id here
Without additional context, I can only recommend that you wait on the element before sending keys to it:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
input = WebDriverWait(driver, 30).until(
EC.visibility_of_element_located((By.XPATH, "//input[#placeholder='User ID']")))
input.send_keys("userId")
Full working sample as requested by asker:
from selenium import webdriver
from time import sleep
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://kite.zerodha.com/connect/login?api_key=b8w63qg9m4c3zubd&sess_id=bW3U1OwidO97o11scfeTbyfX4j5tViNp")
input = WebDriverWait(driver, 30).until(
EC.visibility_of_element_located((By.XPATH, "//input[#placeholder='User ID']")))
input.send_keys("userId")
sleep(10) # this sleep is here so you can visually verify the text was sent.
driver.close()
driver.quit()
The above code has succeeded every time I have run it.

How to locate and click "speed test" link on netflix using selenium in python?

I'm a complete beginner with selenium, sorry if the question is dumb (it is dumb):)
I need to find Speed test link on https://www.netflix.com/ and then click it.
i've tried searching by text and some other options. But nothing seems to work, I don't know why.
from selenium import webdriver
from selenium.webdriver.common.keys import Keysfrom selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("https://www.netflix.com/")
driver.implicitly_wait(10)
elem = driver.find_element_by_link_text("Speed test")
elem.click()
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Sign in"}
(Session info: chrome=75.0.3770.142)
The element with text as Speed Test is out ofthe Viewport so you need to induce WebDriverWait for the desired element to be clickable() and you can use the following Locator Strategy:
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='footer-link']/span[text()='Speed Test']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Use WebDriverWait and element_to_be_clickable with following xpath.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.netflix.com/")
elem = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//span[#data-uia='data-uia-footer-label'][contains(.,'Speed Test')]")))
elem.click()
Browser snapshot:
To added to this answer you need to use WebDriverWait and then click on the element Show more info
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.netflix.com/")
elem = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//span[#data-uia='data-uia-footer-label'][contains(.,'Speed Test')]")))
elem.click()
WebDriverWait(driver,60).until(EC.element_to_be_clickable((By.XPATH,"//a[contains(.,'Show more info' )]"))).click()

how to Login gmail using Python Selenium

Hi all am trying to automate login the gmail account using python but am unable to do so , it just opens the gmail but nothing happens, what am i missing?
from selenium import webdriver
browser = webdriver.Chrome("C:/Users/INCT-KaviChand/Downloads/chromedriver_win32/chromedriver.exe")
browser.get('http://gmail.com')
emailElem = browser.find_element_by_id('Email')
emailElem.send_keys('kavi')
nextButton = browser.find_element_by_id('next')
nextButton.click()
passwordElem = browser.find_element_by_id('kavisam')
passwordElem.send_keys('MyPassword')
signinButton = browser.find_element_by_id('signIn')
signinButton.click()
also tried below one
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Chrome("C:/Users/INCT-KaviChand/Downloads/chromedriver_win32/chromedriver.exe")
browser.get('http://gmail.com')
wait = WebDriverWait(browser, 10)
emailElem = browser.find_element_by_id('Email')
emailElem.send_keys("abc#gmail.com")
password_elem = wait.until(EC.presence_of_element_located((By.ID,'Passwd')))
password_elem.send_keys("xyz")
browser.find_element_by_name('signIn').click()
Try to find element in different way, code below is working for me:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(executable_path="C:/TestFiles/chromedriver.exe")
driver.get('http://gmail.com')
driver.find_element_by_xpath('//input[#type="email"]').send_keys('example', Keys.ENTER)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//input[#type="password"]')))
driver.find_element_by_xpath('//input[#type="password"]').send_keys('example', Keys.ENTER)

Resources