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

法典


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

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()

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。


烙印99
浏览 110回答 2
2回答

qq_花开花谢_0

我刚刚修改了你的代码,现在它的工作原理:from selenium import webdriverfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECimport timechrome = 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')

温温酱

在AUT中模拟动作时,还有其他事情需要注意。即微调器和装载机。所以你也需要处理这些。在你的代码中引入。据观察,在单击“注册”按钮时,它开始显示加载程序,然后显示计划,以便处理下面的代码使用,直到该加载程序被隐藏,然后执行单击所需的计划。ExplicitWaitchrome.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 PlanWebDriverWait(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')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python