简体   繁体   English

硒:没有这样的元素:无法定位元素:

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

I have a question about Selenium.我有一个关于硒的问题。

My idea:我的点子:

My idea is to make a Python script that logs in to this website.我的想法是制作一个登录这个网站的Python脚本。 Selenium sends the username and password to the HTML input field and submits it. Selenium 将用户名和密码发送到 HTML 输入字段并提交。

Problem:问题:

My code keeps saying:我的代码一直在说:

Message: no such element: Unable to locate element:

I have tried this code with google.com for example and that works.例如,我曾在 google.com 上尝试过此代码,并且效果很好。 Why is this not working with this login page?为什么这不适用于此登录页面? Can anybody help me please?有人可以帮我吗?

My Python code:我的 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()

i don't know how it works in pyton, im use js but try to use xpath driver.find_element_by_xpath('your xpath') [maybe use 1 more click) ('xpath element').click() - element is active.我不知道它在 pyton 中是如何工作的,我使用 js 但尝试使用 xpath driver.find_element_by_xpath('your xpath') [也许再使用 1 次点击) ('xpath element').click() - 元素处于活动状态。 and after use send keys ******.send_keys('username')并在使用后发送密钥 ******.send_keys('username')

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

As already explained, the element is in an iframe .如前所述,该元素位于iframe Need to switch to frame to interact with the element.需要switch to frame与元素进行交互。

It would be better apply Explicit waits.最好应用显式等待。

# 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