为了账号安全,请及时绑定邮箱和手机立即绑定

硒 Web 驱动程序 - 无法找到元素?

硒 Web 驱动程序 - 无法找到元素?

烙印99 2022-08-11 17:11:13
法典from selenium import webdriverfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECchrome = webdriver.Chrome(executable_path= 'C:\webdriver.exe\chromedriver.exe',port=9515)url = 'https://protonmail.com/'chrome.get(url)chrome.implicitly_wait(10)chrome.find_element_by_xpath('//*[@class="btn btn-default btn-short"]').click()chrome.find_element_by_class_name("panel-heading").click()chrome.find_element_by_id("freePlan").click()chrome.find_element_by_id('username')chrome.find_element_by_id("password").send_keys('password')chrome.find_element_by_id("passwordc").send_keys('password')断续器<input placeholder="Choose username" required="" name="username" messages="[object Object]" iframename="top" pattern=".{1,40}" id="username" class="input">问题chrome.find_element_by_id('username')我正在尝试输入用户名;但是,python说它找不到该元素,即使我正在使用它给你的用户名id。
查看完整描述

2 回答

?
qq_花开花谢_0

TA贡献1835条经验 获得超7个赞

我刚刚修改了你的代码,现在它的工作原理:


from selenium import webdriver


from selenium.webdriver.common.keys import Keys


from selenium.webdriver.support.ui import WebDriverWait


from selenium.webdriver.support import expected_conditions as EC


import time


chrome = webdriver.Chrome(executable_path= 'C:\webdriver.exe\chromedriver.exe',port=9515)


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


chrome.get(url)


chrome.implicitly_wait(10)


chrome.find_element_by_xpath('//*[@class="btn btn-default btn-short"]').click()


chrome.find_element_by_class_name("panel-heading").click()


chrome.find_element_by_id("freePlan").click()


time.sleep(10)


#chrome.find_element_by_xpath('//*[contains(concat( " ", @class, " " ), concat( " ", "top", " " ))]')


chrome.switch_to.frame(chrome.find_element_by_xpath('//*[contains(concat( " ", @class, " " ), concat( " ", "top", " " ))]'))

typeinput = chrome.find_element_by_xpath('//*[@id="username"]')

typeinput.click()

typeinput.clear()

typeinput.send_keys('password')


chrome.switch_to.default_content()


chrome.find_element_by_id("password").send_keys('password')


chrome.find_element_by_id("passwordc").send_keys('password')


查看完整回答
反对 回复 2022-08-11
?
温温酱

TA贡献1752条经验 获得超4个赞

在AUT中模拟动作时,还有其他事情需要注意。即微调器和装载机。所以你也需要处理这些。


在你的代码中引入。据观察,在单击“注册”按钮时,它开始显示加载程序,然后显示计划,以便处理下面的代码使用,直到该加载程序被隐藏,然后执行单击所需的计划。ExplicitWait


chrome.find_element_by_xpath('//*[@class="btn btn-default btn-short"]').click()

WebDriverWait(chrome, 20).until(EC.invisibility_of_element_located((By.ID, "redir"))

chrome.find_element_by_css_selector("div[aria-controls='plan-free']").click()

在执行单击按钮时,它会重定向到新页面并显示新页面加载在那里,然后加载注册表单。使用下面的代码 -Free Plan


WebDriverWait(chrome, 10).until( EC.invisibility_of_element_located((By.ID, "pm_slow"))

注册表单中的用户名在 iframe 下加载,因此您需要先切换到 iframe,然后执行进一步的操作


username_frame = chrome.find_element_by_xpath("//div[@class='usernameWrap']//iframe[@title='Registration form']")

chrome.switch_to.frame(username_frame)

WebDriverWait(chrome, 10).until( EC.visibility_of_element_located((By.ID, "username"))

chrome.find_element_by_id('username').send_keys(‘username’)

chrome.switch_to.default_content()

chrome.find_element_by_id("password").send_keys('password')

chrome.find_element_by_id("passwordc").send_keys('password')


查看完整回答
反对 回复 2022-08-11
  • 2 回答
  • 0 关注
  • 109 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号