简体   繁体   中英

Selenium, Trying to Find an Element [Python]

I am trying type the username and password into my University mail using Selenium module. This is the id's attribute value that I'm trying to locate:

<input id="ContentPlaceHolder1_UsernameTextBox" name="username" tabindex="1" placeholder="FAU Net ID" value="" autocomplete="false" type="text">

Here's how I'm trying to find it in my program:

userElem = browser.find_element_by_id('ContentPlaceHolder1_UsernameTextBox')

But this is the error that I'm running into:

Exception -:

Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    emailElem = browser.find_element_by_id('ContentPlaceHolder1_UsernameTextBox')
  File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 269, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 752, in find_element
    'value': value})['value']
  File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
    self.error_handler.check_response(response)

  File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [id="ContentPlaceHolder1_UsernameTextBox"]

Thanks and any help would be greatly appreciated.

EDIT:

for reference this is the page

Link

If Selenium is telling you it couldn't find the element, then it isn't there. There are typically a few possible reasons for this:

  1. You did something silly like mistyping the element name. Are you sure that's the correct element ID?
  2. You aren't on the page you thought you were. Are you sure you're on the right page? Have you looked at browser.page_source to double-check?
  3. The element isn't there when the page loads, but is added to the page later via Javascript, perhaps after a set time delay or when some event happens such as a mouse click. Does the page have javascript like this?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM