简体   繁体   English

Python Selenium无法找到用户名元素

[英]Python selenium unable to locate username element

I am trying to create an automated login system for my local library website ( http://mcls.ent.sirsi.net/client/en_US/mclweb ) using Python and Selenium. 我正在尝试使用Python和Selenium为我的本地图书馆网站( http://mcls.ent.sirsi.net/client/en_US/mclweb )创建一个自动登录系统。 However, my script is unable to find the username box. 但是,我的脚本无法找到用户名框。

The HTML from the website for the username box looks like this 网站上用于用户名框的HTML如下所示

<input maxlength="30" class="user_name_input" id="j_username" name="j_username" type="text">

and this is the code I used to find it 这是我用来找到它的代码

username = browser.find_element_by_id('j_username')
username.send_keys(u)

however, I am getting the following error: 但是,我收到以下错误:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"j_username"}

Am I supposed to use a different function? 我应该使用其他功能吗? Or did I use find_element_by_id() wrong? 还是我使用find_element_by_id()错误? Thanks in advance. 提前致谢。

U can also login to that site without using iframe 您也可以在不使用iframe的情况下登录该网站

 browser.get("http://mcls.ent.sirsi.net/client/en_US/login") browser.find_element_by_id('j_username').send_keys(u) 

The login form lies on a frame. 登录表单位于框架上。 You have to switch to the frame. 您必须切换到框架。

browser.get("http://mcls.ent.sirsi.net/client/en_US/mclweb")
browser.find_element_by_class_name('loginLink').click()
time.sleep(5)
browser.switch_to.frame(1) //login iframe is the second frame in the page
time.sleep(5)
browser.find_element_by_id('j_username').send_keys(u)

Alternative way, in case you want to do without switching to frame: 或者,如果您不想切换到框架,可以执行以下操作:

browser.get("http://mcls.ent.sirsi.net/client/en_US/mclweb/search/patronlogin")
browser.find_element_by_id('j_username').send_keys(u)

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

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