Password field not taking keys using Selenium through Python - python

I am trying to send keys via selenium it is taking for username but not for password.
I have tried clicking and sending keys then.
HTML of password field:
<div>
<input name="txtPassword" type="password" id="txtPassword" required="" style="margin-bottom:0px;" class="blur">
<a id="btnSmallForgotPassword" class="smallForgotPassword visible-sm-block" href="javascript:__doPostBack('btnSmallForgotPassword','')">forgot password</a>
</div>
WebDriverWait(driver, 3).until(EC.visibility_of_element_located((By.ID,"txtPassword"))).click()
WebDriverWait(driver, 3).until(EC.visibility_of_element_located((By.ID,"txtPassword"))).send_keys("san")
I am getting no error message but it is not sending keys for password

try with this code :
WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.ID,"txtPassword"))).send_keys('your_password')
You should not create object of WebDriverWait too many times, use it like this :
wait = WebDriverWait(driver,10)
wait.until(EC.element_to_be_clickable((By.ID,"txtPassword"))).click()
wait.until(EC.element_to_be_clickable((By.ID,"txtPassword"))).send_keys('your_password')
and wait.until(EC.element_to_be_clickable((By.ID,"txtPassword"))) returns a web element, where you can use methods like click(), clear(), send_keys() etc.
You can write your code like this also :
password_field = wait.until(EC.element_to_be_clickable((By.ID,"txtPassword")))
password_field.click()
password_field.send_keys('your_password')
EDIT1 :
You can use this css selector :
input[id='txtPassword']
EDIT 2 :
You can use this full method :
wait = WebDriverWait(driver,10)
driver.maximize_window()
driver.get("http://backgriduk.medialava.com/pages/Staff/Login.aspx?LANGUAGE_ID=3&O=7")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input[id='txtUser']"))).send_keys("abhishek")
wait.until(EC.element_to_be_clickable((By.ID,"txtPassword"))).click()
wait.until(EC.element_to_be_clickable((By.ID,"txtPassword"))).send_keys('your_password')

Try this:
from selenium import webdriver
import time
driver = webdriver.Chrome('/usr/bin/chromedriver')
driver.get('http://backgriduk.medialava.com/pages/Staff/Login.aspx?LANGUAGE_ID=3&O=7')
time.sleep(3)
username = driver.find_element_by_id("txtUser")
username.clear()
# insert username
username.send_keys("mrcats")
password = driver.find_element_by_name("txtPassword")
password.clear()
# insert password
password.send_keys("catskillz")
login = driver.find_element_by_name("btnLogin")
#click on login button
login.click()

The above code should work.However I have tested following code on chrome browser and working fine.
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('http://backgriduk.medialava.com/pages/Staff/Login.aspx?LANGUAGE_ID=3&O=7')
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.ID,'txtUser'))).send_keys("Abhishek")
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'div.divInnerRightControls #txtPassword'))).send_keys("Abhishek")
Output:

To send character sequence to the USER ID and PASSWORD field you need to to induce WebDriverWait for the element to be clickable and you can use the following Locator Strategy:
Code Block:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get("http://backgriduk.medialava.com/pages/Staff/Login.aspx?LANGUAGE_ID=3&O=7")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input#txtUser"))).send_keys("abhishek_gupta")
driver.find_element_by_css_selector("input#txtPassword").send_keys("abhishek_gupta")
Browser Snapshot:

Related

Unable to find input element with Selenium and Python

I am new in selenium and python.
I trying to find element using selenium but no matter what I tried (xpath, CSS) I always got the following message:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element
Here the code I wrote :
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from time import sleep
from requests_html import HTML
options = Options()
# options.add_argument("--headless")
options.add_argument('disable-notifications')
options.add_argument('--disable-infobars')
options.add_argument('--disable-blink-features=AutomationControlled')
driver = webdriver.Chrome(executable_path=r'C:/Users/XXX/PycharmProjects/chromedriver.exe', options=options)
first_url = "https://www.micromania.fr/jeu-concours-summer-show.html"
driver.get(first_url)
# For the following, it works
refuse = driver.find_element_by_xpath('//*[#id="truste-consent-required"]').click()
time.sleep(2)
But when I tried to do a driver.find_element_by -CSS, Xpath, to find for the mail field to be able to fill it, I met the error message.
HTML of the mail field I tried to reach is there:
<input data-v-0f8f86ae="" type="email" placeholder="Email*" class="input">
UPDATE
You need to first click on the account icon, having class sidebar-login header-link-item icon-account no-decoration color-white. That click will do a XHR network call, which will bring some new elements into page, including email address field. Also, it's wise to wait for elements to load in page before searching them, instead of just searching for them. Also, it's wise to wrap the cookie button dismissal in a try/except block, just in case it's not popping up all the time.
Adapting the following snippet to your code will get you what you're after:
url='https://www.micromania.fr/jeu-concours-summer-show.html'
browser.get(url)
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".sidebar-login.header-link-item.icon-account.no-decoration.color-white"))).click()
email_field = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#title='Adresse email']")))
email_field.click()
email_field.send_keys('my amazing email address')
print('done')
EDIT: for the email field I'm not able to see (restricted to French IPs only):
email_2_field = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='home']/input[#type='email']")))
email_2_field.click()
Given the HTML:
<input data-v-0f8f86ae="" type="email" placeholder="Email*" class="input">
As the desired element is a dynamic element, to send a character sequence within the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.input[type='email'][placeholder^='Email']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='input' and starts-with(#placeholder, 'Email')][#type='email']"))).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

Python Selenium program can't locate html element using XPATH or class name within twitter login page. Is it a problem with my explicit wait?

What I want to do:
Build a tweet bot for twitter.
My problem:
My program is unable to find the text box to enter my username, even when I use explicit wait.
What I've tried:
Locating the element by class
Locating the element by name
Locating the element by xpath
Using explicit wait to make sure the element is on the screen before program continues.
My code:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
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
# getting webdriver path
s = Service('/Users/shannonslater/Desktop/Everything/Dev/WebDrivers/chromedriver101')
driver = webdriver.Chrome(service=s)
# navigating to twitter login page
driver.get('https://twitter.com/login')
# trying to click on the username text box
try:
username_text_box = WebDriverWait(driver, 20).until(
EC.presence_of_element_located(By.XPATH, '//*[#id="layers"]/div/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div/div[5]/label/div/div[2]/div/input')
)
username_text_box.click()
except:
print("could not find username_text_box element")
# username text box is never clicked and "could not find username" is always printed
I am copying the xpath directly from the inspected html element:
The username field on Twitter Login Page is a dynamic element.
<input autocapitalize="sentences" autocomplete="username" autocorrect="on" name="text" spellcheck="true" type="text" dir="auto" class="r-30o5oe r-1niwhzg r-17gur6a r-1yadl64 r-deolkf r-homxoj r-poiln3 r-7cikom r-1ny4l3l r-t60dpp r-1dz5y72 r-fdjqy7 r-13qz1uu" value="">
Solution
To send a character sequence to the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
options = Options()
options.add_argument("start-maximized")
s = Service('C:\\BrowserDrivers\\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)
driver.get("https://twitter.com/i/flow/login")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[autocomplete='username']"))).send_keys("ShannonSlater")
Using XPATH:
options = Options()
options.add_argument("start-maximized")
s = Service('C:\\BrowserDrivers\\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)
driver.get("https://twitter.com/i/flow/login")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#autocomplete='username']"))).send_keys("ShannonSlater")
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
Browser Snapshot:

How to find the element Name="Password" to change the value to my current password

from selenium import webdriver
from selenium.webdriver.common.by import *
from selenium.webdriver.support.ui import *
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import time
from lib2to3.pgen2 import driver
usernameStr = '#email.com'
passwordStr = '*******'
browser = webdriver.Chrome()
browser.get('https://ebill.nationalfuelgas.com/cgi/natfuel-bin/vortex.cgi')
browser.maximize_window()
# fill in username and hit the next button
time.sleep(3)
browser = browser.find_element_by_name("login")
browser.send_keys(usernameStr)
browser.send_keys(Keys.TAB, Keys.TAB)
time.sleep(5)
# Wait for transition then continue to fill items
I am trying to get this code figure out please help
password = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.NAME, "password")))
password.send_keys(passwordStr)
time.sleep(1)
I have tried to Tag Name, Name etc and still is not doing much every other code works but except this one.
# This code is to login the website
slogin = browser.find_element_by_name("continue" + Keys.ENTER)
Snapshot:
To send a character sequence to the Email and Password field using Selenium you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("https://ebill.nationalfuelgas.com/cgi/natfuel-bin/vortex.cgi")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.inputfield[alt='Email']"))).send_keys("shadownet96")
driver.find_element_by_css_selector("input.inputfield[alt='Password']").send_keys("shadownet96")
Using XPATH:
driver.get("https://ebill.nationalfuelgas.com/cgi/natfuel-bin/vortex.cgi")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='inputfield' and #alt='Email']"))).send_keys("shadownet96")
driver.find_element_by_xpath("//input[#class='inputfield' and #alt='Password']").send_keys("shadownet96")
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
Browser Snapshot:

How to send character sequence to the username and password field within the url through Selenium?

I tried to log in in https://login.economicmodeling.com/login/login.php, but the username I put in doesn't show up, and after I run the command to fill in password, it automatically opened a new tab without actually filling in password. Anyone can help? Thanks!
from selenium import webdriver
driver = webdriver.Safari()
driver.get("https://login.economicmodeling.com/login/login.php")
driver.find_element_by_class_name("cc-btn cc-dismiss").click()
user = driver.find_element_by_css_selector('input[name = user]')
password = driver.find_element_by_css_selector('input[name = password]')
user.clear()
user.send_keys('xxx')
password.clear()
password.send_keys('xxx')
driver.find_element_by_id("submitbutton").click()
Missing single quotes around 'user' and 'password' attributes.
user = driver.find_element_by_css_selector("input[name='user']")
password = driver.find_element_by_css_selector("input[name='password']")
Sending the form should be done with submit, not click.
driver.find_element_by_id("submitbutton").submit()
As per your question to login in https://login.economicmodeling.com/login/login.php you need to induce WebDriverWait for the desired elements to be clickable and you can use the following solution:
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
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("https://login.economicmodeling.com/login/login.php")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.fancy.form#userinput"))).send_keys('xxx')
driver.find_element_by_css_selector('input.fancy.form#passwordinput').send_keys('xxx')
driver.find_element_by_css_selector("input.submit.button.success#submitbutton").click()
Browser Snapshot:

How to login/submit request through Selenium and Python

I am not sure why selenium is not sending submit request.
edx.py or Coursera
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://courses.edx.org/login')
email = browser.find_element_by_id('login-email')
email.send_keys('xxxxx#gmail.com')
pwd = browser.find_element_by_id('login-password')
pwd.send_keys('password')
login_attempt = browser.find_element_by_xpath('//*[#id="login"]/button')
login_attempt.submit()
try login_attempt.click()
You form not has action attribute, so the form.submit() won't know the destination to submit.
So for safe purpose, recommend to find the button and click on it. Rather than use the convenient API: element.submit().
You can try with below CSS Selector
action.action-primary.action-update.js-login.login-button
Update
Just noticed that you have missing dot (.) in your implementation
browser.find_element_by_xpath('.//*[#id='login']/button')
As per your code trials to populate the Email and Password field and click() on Sign in button you need to induce WebDriverWait for the elements to be clickable and you can use the following code block:
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
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("https://courses.edx.org/login")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.input-block#login-email"))).send_keys("Sakim#gmail.com")
driver.find_element_by_css_selector("input.input-block#login-password").send_keys("Sakim")
driver.find_element_by_css_selector("button.action.action-primary.action-update.js-login.login-button").click()
Browser Snapshot:

Categories