Noob
Noob

Reputation: 13

log on to a webpage using selenium

I am totally new to this.

I was following examples I could find here and wrote the following:

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

thisDirectory = 'my directory'

waitTime = 20

URL_entry = "https://www.capitaliq.com"
username = "my username"
password = "my password"

browser = webdriver.Chrome(executable_path=thisDirectory + 
'\\chromedriver.exe')  
browser.get(URL_entry)

userNameBox = WebDriverWait(browser, waitTime).until(
EC.presence_of_element_located((By.XPATH, '//input[@id="txtUsername"]'))
)
userNameBox.send_keys(username)

passwordBox = WebDriverWait(browser, waitTime).until(
EC.presence_of_element_located((By.XPATH, '//input[@id="txtPwd"]'))
)
passwordBox.send_keys(password)
loginButton = WebDriverWait(browser, waitTime).until(
EC.presence_of_element_located((By.XPATH, '//input[@name="butSignin"]'))
)
loginButton.click()
time.sleep(waitTime) 

I also tried a few other slightly different methods but always getting the error message: raise TimeoutException(message, screen, stacktrace) TimeoutException: Message:

This is my first time posting here, sorry if I did anything inappropriate. Any help will be appreciated. Thanks a lot.

Upvotes: 1

Views: 505

Answers (1)

Andersson
Andersson

Reputation: 52665

You use wrong locators: there are no elements with id attributes "txtUsername", "txtPwd", "butSignin" on https://www.capitaliq.com login page...

Try below lines instead:

userNameBox = WebDriverWait(browser, waitTime).until(
EC.presence_of_element_located((By.ID, 'username'))
)

userNameBox.send_keys(username)

passwordBox = WebDriverWait(browser, waitTime).until(
EC.presence_of_element_located((By.ID, 'password'))
) 

passwordBox.send_keys(password)

loginButton = WebDriverWait(browser, waitTime).until(
EC.presence_of_element_located((By.ID, 'myLoginButton'))
)

loginButton.click()

Note that locators for elements in authorization form are not the same for all web-sites. To get exact locators you can do

right-click on element in browser -> select "Inspect element" -> check source code of target element... There you can find unique id/name values or other attributes that you can use to locate element

Upvotes: 1

Related Questions