【问题标题】:Selenium No Element Found Exception even after copying element from source codeSelenium No Element Found Exception 即使在从源代码复制元素后
【发布时间】:2019-01-30 04:24:01
【问题描述】:

我正在尝试简单地登录到此页面以访问 LexisNexis。这是我的代码:

from selenium import webdriver
browser = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver')
type(browser)

browser.get('https://sfx.carli.illinois.edu/sfxuiu?url_ver=Z39.88-2004&url_ctx_fmt=infofi/fmt:kev:mtx:ctx&ctx_enc=info:ofi/enc:UTF-8&ctx_ver=Z39.88-2004&rfr_id=info:sid/sfxit.com:azlist&sfx.ignore_date_threshold=1&rft.object_id=63750000000001351&svc.fulltext=yes')
linkElem2 = browser.find_element_by_link_text('LEXIS NEXIS DATABASES') 
type(linkElem2)
linkElem2.click()
alert = browser.switch_to.alert
alert.accept()

loginElem = browser.find_element_by_id('j_username')
loginElem.send_keys('username')
passElem = browser.find_element_by_id('j_password')
passElem.send_keys('password')
passElem.submit()

这里是html源代码:

div class="form_row" 标签为 =“j_username” 输入你的 strongNetID:/strong/label 输入 id="j_username" name="j_username" autocomplete="OFF" autocapitalize="off" autocorrect="off" value="" type="text" /div

我不确定我做错了什么:/

【问题讨论】:

    标签: html selenium exception


    【解决方案1】:

    1.问题 “LEXIS NEXIS DATABASES”链接打开新窗口,您必须切换到它才能登录,Handle multiple windows in Python

    from selenium import webdriver
    
    browser = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver')
    browser.implicitly_wait(5)
    
    browser.get('https://sfx.carli.illinois.edu/sfxuiu?url_ver=Z39.88-2004&url_ctx_fmt=infofi/fmt:kev:mtx:ctx&ctx_enc=info:ofi/enc:UTF-8&ctx_ver=Z39.88-2004&rfr_id=info:sid/sfxit.com:azlist&sfx.ignore_date_threshold=1&rft.object_id=63750000000001351&svc.fulltext=yes')
    browser.find_element_by_link_text('LEXIS NEXIS DATABASES').click()
    
    browser.close()
    browser.switch_to.window(browser.window_handles[0])
    
    browser.find_element_by_id('j_username').send_keys('username')
    passElem = browser.find_element_by_id('j_password')
    passElem.send_keys('password')
    passElem.submit()
    

    2。问题 Link您在cmets 中分享的第一次打开时出现错误(新的浏览器配置文件)。 您只需打开page 并单击“使用您的伊利诺伊州网络ID 登录”即可成功打开登录页面。这种方式好多了,因为你不需要处理windows,检查下面的代码:

    from selenium import webdriver
    
    browser = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver')
    browser.implicitly_wait(5)
    
    browser.get('https://compass2g.illinois.edu/webapps/login/')
    browser.find_element_by_link_text('Login with your Illinois NetID').click()
    
    browser.implicitly_wait(5)
    
    browser.find_element_by_id('j_username').send_keys('username')
    passElem = browser.find_element_by_id('j_password')
    passElem.send_keys('password')
    passElem.submit()
    

    【讨论】:

    • 仍然无法正常工作:/ 我什至运行了书中的确切代码,使无聊的东西自动化,登录到电子邮件的那个,它给出了同样的错误!未找到元素!
    • 提供网址或html
    • 检查 iframe 标签内是否有输入
    • 太棒了!这帮助很大!!!!我不得不将 passElem 行更改为类似于 browser.find_element_by_id 行,但仅此而已。再次感谢,我花了几个小时试图解决这个问题
    【解决方案2】:

    单击该链接会打开一个新选项卡,因此您需要先切换到新选项卡,然后才能找到用户名和密码字段。试试这个(缩进可能被破坏):

    from selenium import webdriver
    import time
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    browser = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver')
    browser.get('https://sfx.carli.illinois.edu/sfxuiu?url_ver=Z39.88-2004&url_ctx_fmt=infofi/fmt:kev:mtx:ctx&ctx_enc=info:ofi/enc:UTF-8&ctx_ver=Z39.88-2004&rfr_id=info:sid/sfxit.com:azlist&sfx.ignore_date_threshold=1&rft.object_id=63750000000001351&svc.fulltext=yes')
    linkElem2 = browser.find_element_by_link_text('LEXIS NEXIS DATABASES') 
    linkElem2.click()
    alert = browser.switch_to.alert  
    alert.accept()
    time.sleep(5) #to add 5 sec wait, you can use other wait methods for window handle. e.g. https://stackoverflow.com/questions/26641779/python-selenium-how-to-wait-for-new-window-opens
    browser.switch_to.window(browser.window_handles[1])
    WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, 'j_username')))
    loginElem = browser.find_element_by_id('j_username')
    loginElem.send_keys('username')
    passElem = browser.find_element_by_id('j_password')
    passElem.send_keys('password')
    passElem.submit()
    

    您将需要以下导入

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    

    【讨论】:

    • 谢谢!我仍然收到错误。首先它说没有定义名称“驱动程序”,所以我改用浏览器。然后它说列表索引超出范围,所以我将其更改为0。然后它说名称'等待'未定义,所以我使用:
    • from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException
    • 正确,等待方法需要导入,请查看我的更新答案
    • 我使用了导入,但我不断收到超时异常。我尝试了 10,20,30,60 但仍然无法正常工作:/
    • 我不确定这是否意味着它仍然找不到元素。也许元素被隐藏了?但是我不知道怎么做才能让python找到!!
    猜你喜欢
    • 2014-07-23
    • 1970-01-01
    • 1970-01-01
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 2012-09-16
    相关资源
    最近更新 更多