[您好,我试图使用Selenium和Python为网站https://mail.protonmail.com/create/new?language=en键入用户名字段。
通过开发人员工具,我可以使用CSSSelector / Xpath或其他方式检查项目。但是,当我运行pthon脚本时,它不起作用。截图截图:
我的代码如下:
BASE_URL = 'https://mail.protonmail.com/create/new?language=en'
driver = webdriver.Chrome(executable_path='./drivers/chromedriver')
driver.get(BASE_URL)
river.find_element_by_xpath('//*[@id="username"]').send_keys('someStringValue')
并且在执行以下代码后,出现此错误:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="username"]"}
(Session info: chrome=83.0.4103.97)
有任何建议吗?
driver.switchTo().frame(n);
编辑:如果您阅读TOS,将会看到
此服务仅向人员提供。通过“自动程序”或自动方法注册的帐户未经授权,将被终止。
<iframe>
内,因此您必须:将WebDriverWait
设置为所需的[[框架可用并切换到它]]。
CSS_SELECTOR
:driver.get('https://mail.protonmail.com/create/new?language=en')
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"div.usernameWrap iframe[title='Registration form']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.input#username"))).send_keys("FunnyBoss")
XPATH
:driver.get("https://www.nasdaq.com/symbol/msft/interactive-chart?timeframe=5d")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//div[@class='usernameWrap']//iframe[@title='Registration form']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='input' and @id='username']"))).send_keys("FunnyBoss")
注
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
浏览器快照:
您可以在以下位置找到相关的讨论: