提问人:Olga 提问时间:5/19/2023 最后编辑:ShawnOlga 更新时间:5/19/2023 访问量:98
Python Selenium:NoSuchElementException:消息:没有此类元素:无法找到元素:
Python Selenium: NoSuchElementException: Message: no such element: Unable to locate element:
问:
我有一个网站,我需要登录:
https://gb.clientportal.dunnhumby.com/bkr
我曾经这样做过。检查页面时,登录输入字段具有以下属性:Selenium
<input id="userNameInput" name="UserName" type="email" value="" tabindex="1" class="text fullWidth" spellcheck="false" placeholder="[email protected]" autocomplete="off">
我会使用以下内容:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get('https://gb.clientportal.dunnhumby.com/bkr')
username = driver.find_element(By.CSS_SELECTOR,"#userNameInput")
username.send_keys('test_user')
它返回一个错误:NoSuchElementException
我试图通过所有可能的选项找到元素:, - 一切都给出错误。id
name
然后我尝试了等待:
driver = webdriver.Chrome()
driver.get('https://gb.clientportal.dunnhumby.com/bkr')
wait = WebDriverWait(driver, 10)
username_field = wait.until(EC.visibility_of_element_located((By.XPATH, '//input[@id="userNameInput"]')))
错误是 Like,即使等待后,元素仍未找到。TimeoutException: Message: Stacktrace:Backtrace:
然后我试图检查,但该 URL 上没有。iframe
iframe
driver.find_element(By.TAG_NAME, "iframe")
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"tag name","selector":"iframe"}
有什么想法,我还能尝试什么?
谢谢
答:
0赞
Shawn
5/19/2023
#1
有一个 FRAME,其中包装了所需的元素。您首先需要切换到 FRAME,然后执行操作。参考以下工作代码:
driver.get("https://gb.clientportal.dunnhumby.com/bkr")
wait = WebDriverWait(driver, 10)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "(//frame)[1]")))
username = driver.find_element(By.CSS_SELECTOR,"#userNameInput")
username.send_keys('test_user')
所需进口:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
结果:
使用 FRAME 执行操作后,您可以使用以下代码切换回主要内容:
driver.switchTo().defaultContent();
以下是 FRAME 的 HTML 供您参考:
评论