Python Selenium:NoSuchElementException:消息:没有这样的元素:无法找到元素:

问题描述 投票:0回答:1

我有一个需要登录的网站:

'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"]')))

错误是

TimeoutException: Message: Stacktrace:Backtrace:
就像即使在等待元素仍然没有找到一样。

然后我尝试检查 iframe,但是那个 URL 上没有 iframe。

driver.find_element(By.TAG_NAME, "iframe")
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"tag name","selector":"iframe"}

还有什么我可以尝试的想法吗?

谢谢

python selenium-webdriver nosuchelementexception
1个回答
0
投票

有一个 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,供您参考:

© www.soinside.com 2019 - 2024. All rights reserved.