Zanam
Zanam

Reputation: 4807

Selenium unable to find username and password field

I am trying to log into a website by passing in username and password. I am using the following code (Python 3.5):

from selenium import webdriver
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.common.by import By
url2 = 'https://services.yesenergy.com/PS/TimeSeriesAnalysis.html#d=a&title=Time Series Analysis'
driver = webdriver.Chrome(r'C:\chromedriver.exe')
driver.get(url2)
username = driver.find_element_by_id("Username")

But I get the error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"Username"}
  (Session info: chrome=63.0.3239.132)
  (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.15063 x86_64)

Edit: I already tried the wait method to ensure the page is properly loaded but that doesn't work either

Upvotes: 0

Views: 1228

Answers (1)

aylr
aylr

Reputation: 369

Your instincts to check for page load are great, however, please look carefully at the html element you are trying to select:

<input placeholder="Username" type="text" name="j_username">

Notice that it does not have an id attribute, therefore you need to use another selection method.

An easy way to troubleshoot these kinds of problems interactively is to learn about using Chrome (or other browser) web dev tools, jQuery selectors and CSS selectors. Then you can choose the appropriate selenium method (find_element_by_css_selector).

For example, open up your browser's dev tools and paste this into the console:

$('input[name="j_username"]')

You will see that the correct element is selected. Now try using a method like this:

find_element_by_css_selector('input[name="j_username"]')

Upvotes: 2

Related Questions