問題描述
您好,我正在嘗試使用 Selenium 和 Python 為網(wǎng)站
我的代碼如下:
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')
執(zhí)行以下代碼后,出現(xiàn)錯(cuò)誤:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"http://*[@id="username"]"}(會(huì)話信息:chrome=83.0.4103.97)
有什么建議嗎?
Email Address 字段位于 中,因此您必須:
誘導(dǎo) WebDriverWait 使所需的框架可用并切換到它.
誘導(dǎo) WebDriverWait 使所需的元素可點(diǎn)擊.
您可以使用以下任一
參考
您可以在以下位置找到相關(guān)討論:
- iframe下#document的處理方式
- 通過 Selenium 和 python 切換到 iframe
Hi I was trying to type the username field using Selenium and Python for the website https://mail.protonmail.com/create/new?language=en.
From the developer tool, I am able to inspect the item using CSSSelector/Xpath or other way. But when I am running the pthon script its not working. Screenshot attached:
My code is like the following:
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')
And after executing the following code, geetting the error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"http://*[@id="username"]"} (Session info: chrome=83.0.4103.97)
Any suggestion?
解決方案The Email Address field is within an
<iframe>
so you have to:Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
Using
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")
Using
XPATH
:driver.get("https://mail.protonmail.com/create/new?language=en") 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")
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:
Reference
You can find a relevant discussion in:
- Ways to deal with #document under iframe
- Switch to an iframe through Selenium and python
這篇關(guān)于無法使用 Selenium 和 Python 在 ProtonMail 注冊(cè)頁面的用戶名字段中輸入的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!