【问题标题】:Selenium can type in fields but is unable to type in a specific field [duplicate]Selenium 可以输入字段但无法输入特定字段 [重复]
【发布时间】:2020-12-27 14:54:12
【问题描述】:

driver.find_element_by_id('username').send_keys('thisIsAString') Selenium 可以输入密码字段,但不能输入用户名字段。我对它们都使用了相同的代码,但由于某种原因,用户名的行为很奇怪。

<input placeholder="Choose username" required="" name="username" messages="[object Object]" iframename="top" pattern=".{1,40}" id="username" class="input">

这是用户名字段的 HTML
任何帮助将不胜感激!

编辑:
代码:

from selenium import webdriver
import time

url = 'https://protonmail.com/signup'

driver = webdriver.Chrome('chromedriver')
driver.get(url)



time.sleep(2)

driver.find_element_by_class_name('panel-heading').click()

time.sleep(4)

driver.find_element_by_id('freePlan').click()

time.sleep(20)

driver.find_element_by_id('username').send_keys('thisIsAString')

time.sleep(1.5)

driver.find_element_by_id('password').send_keys('passwordForUser')

time.sleep(2)

driver.find_element_by_id('passwordc').send_keys('passwordForUser')

time.sleep(2)

driver.find_element_by_class_name('signUpProcess-btn-create').click()

time.sleep(1)

driver.find_element_by_id('confirmModalBtn').click()

错误消息:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"username"}(第 21 行)

【问题讨论】:

  • 很难说。也许你可以提供一些代码。是否显示任何错误消息?也许它最后就像缺少分号一样简单。
  • 加载页面时,用户名输入字段的加载速度不如密码字段。我强烈认为问题在于输入字段尚未加载,但代码已经在访问它。在访问其内容之前,请确保您的页面已完全加载。

标签: python html selenium browser-automation


【解决方案1】:

我可以看到您的页面中有几个 iFrame。此外,由于它是一个 Ajax 页面,许多组件是单独加载的,所以最好的选择是 explicitwait.Note,time.sleep() 不是一种非常可靠的等待方法,它会使您的测试变得不可预测。使用以下代码:

driver.get("https://protonmail.com/signup")
#Wait for upto 30 sec for Free plan div to appear
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//div[@aria-controls='plan-free']"))).click()

#Wait for upto 30 sec for Select Free plan button to appear
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.ID, "freePlan"))).click()
#Wait and switch to iframe containing user id field
WebDriverWait(driver, 30).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[@title ='Registration form' and @class='top']")))
WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.ID, "username")))
driver.find_element_by_id('username').send_keys("abc")
driver.switch_to.default_content() # Switch to default window
driver.find_element_by_id('password').send_keys("abc")
driver.find_element_by_id('passwordc').send_keys("abc")

请注意,如果您想输入辅助邮箱并点击创建帐户按钮,您还需要切换一个 iframe。在下面使用:

#To enter recovery Email and click on create account button
WebDriverWait(driver, 30).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[@title ='Registration form' and @class='bottom']")))
driver.find_element_by_id('notificationEmail').send_keys("xyz")
driver.find_element_by_name('submitBtn').click()
driver.switch_to.default_content()

【讨论】:

    【解决方案2】:

    您的输入字段尚未加载,但代码正在访问它。您可以使用以下代码等到您的输入字段被加载。然后你就可以访问它了。

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    try:
        element = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.ID, "username"))
        )
    finally:
        driver.quit()
    

    在访问页面上的任何元素之前,通常始终确保您的页面已完全加载。


    更新: 我刚刚看到您在这里使用的是 iFrame。尝试将 ID 添加到 iFrame 标记,然后等待它像这样加载:

    HTML:

    <iframe id="iFrameTagID" title="Registration form" scrolling="no" class="top" data-name="top" sandbox="allow-scripts allow-same-origin allow-popups allow-top-navigation" src="https://secure.protonmail.com/abuse.iframe.html?name=top"></iframe>

    硒:

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    try:
        element = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.ID, "iFrameTagID"))
        )
    finally:
        driver.quit()
    

    【讨论】:

    • 现在卡在加载了?
    • 现在它给了我这个错误:raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:
    • 我刚刚看到您在这里使用的是 iFrame。当然,在这种情况下它不会起作用。你可以在 iFrame 标签中添加一个 ID 吗?看看我编辑的答案。
    • 哦,好吧,我试试看!
    • File "C:\Users\Ro-Fi\Desktop\Emails\emails.py", line 26, in <module> element = WebDriverWait(driver, 10).until( File "C:\Users\Ro-Fi\AppData\Roaming\Python\Python38\site-packages\selenium\webdriver\support\wait.py", line 80, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: 给我这个错误
    猜你喜欢
    • 1970-01-01
    • 2020-04-25
    • 1970-01-01
    • 1970-01-01
    • 2021-10-17
    • 2019-12-27
    • 1970-01-01
    相关资源
    最近更新 更多