如何解决硒可以在字段中键入,但不能在特定字段中键入
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行)
解决方法
我可以看到您的页面中有几个iFrame。另外,由于它的Ajax页面上有许多组件是分别加载的,因此最好的选择是explicitwait
。请注意,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,"//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()
,
您的输入字段尚未被引用,但是代码正在访问它。您可以使用以下代码来等待直到输入字段被加载。然后您可以访问它。
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,"iFrameTagID"))
)
finally:
driver.quit()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。