简体   繁体   English

硒,试图寻找元素[Python]

[英]Selenium, Trying to Find an Element [Python]

I am trying type the username and password into my University mail using Selenium module. 我正在尝试使用Selenium模块在我的大学邮件中输入用户名和密码。 This is the id's attribute value that I'm trying to locate: 这是我要查找的id的属性值:

<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. 如果Selenium告诉您找不到元素,则该元素不存在。 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? 确定这是正确的元素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? 您是否查看过browser.page_source进行了仔细检查?
  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. 当页面加载时,该元素不存在,而是稍后通过Javascript添加到页面中,可能是在设置的时间延迟后或发生某些事件(例如单击鼠标)之后。 Does the page have javascript like this? 页面上是否有这样的JavaScript?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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