Issue with login using selenium and python - python-3.x

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.

Related

Webdriver Selenium click a <li> element inside an Iframe

[![enter image description here][1]][1]trying to click and select the marked
element.
https://i.stack.imgur.com/cIRn8.png
#The options - https://i.stack.imgur.com/e66m1.png
using
from selenium import webdriver
from time import sleep
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Current attempt:
driver.switch_to.frame(driver.find_element_by_id("moduleManagement")) driver.find_element_by_class_name("ranges").find_element_by_tag_name("ul").find_element_by_tag_name("li").click()
I need help with how to select a
element . where i worng
I would advise you to use explicit wait to switch to frame and interact with them :
to click on second li tag, below code should work for you.
wait = WebDriverWait(driver, 10)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "moduleManagement")))
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#colorbox+div[class^='daterangepicker']"))).click()
second_li_element = wait.until(EC.visibility_of_element_located((By.XPATH, "(//li[#data-range-key])[2]")))
second_li_element.click()
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

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)

Is it possible to validate when an entered password is wrong?

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

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