簡體   English   中英

硒:沒有這樣的元素:無法定位元素:

[英]Selenium: no such element: Unable to locate element:

我有一個關於硒的問題。

我的點子:

我的想法是制作一個登錄這個網站的Python腳本。 Selenium 將用戶名和密碼發送到 HTML 輸入字段並提交。

問題:

我的代碼一直在說:

Message: no such element: Unable to locate element:

例如,我曾在 google.com 上嘗試過此代碼,並且效果很好。 為什么這不適用於此登錄頁面? 有人可以幫我嗎?

我的 Python 代碼:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time


login_URL = "https://nl.infothek-sptk.com/isps/infothek/?1043"


driver = webdriver.Chrome()
driver.get(login_URL)

time.sleep(5)

inputElement = driver.find_element_by_name('uname')
inputElement.send_keys(username)

time.sleep(20)

driver.close()

我不知道它在 pyton 中是如何工作的,我使用 js 但嘗試使用 xpath driver.find_element_by_xpath('your xpath') [也許再使用 1 次點擊) ('xpath element').click() - 元素處於活動狀態。 並在使用后發送密鑰 ******.send_keys('username')

driver.switchTo().frame(driver.findElement(By.xpath('xpath frame'))) - 在 js 中

如前所述,該元素位於iframe 需要switch to frame與元素進行交互。

最好應用顯式等待。

# Imports required:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver.get("https://nl.infothek-sptk.com/isps/infothek/?1043")

wait = WebDriverWait(driver,30)

wait.until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"body_frame")))

wait.until(EC.element_to_be_clickable((By.NAME,"uname"))).send_keys("username@mail.com")
# Code to enter other fields.

# Switch back to default to interact with elements outside the iframe.
driver.switch_to.default_content()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM