硒找不到元素,但是元素在https://login.aliexpress.com/网页上 [英] Selenium can't find element, but element is on the https://login.aliexpress.com/ webpage
问题描述
在网站上,硒脚本无法找到登录名和密码字段.我试图通过xpath,css选择器,名称和类名称进行搜索.但没有任何效果.
On the website the selenium script cannot find the login and password fields. I tried to search by xpath, css selector, name and class name. But nothing worked.
from selenium import webdriver
from time import sleep
driver = webdriver.Firefox()
driver.get("https://login.aliexpress.com/")
driver.find_element_by_id("fm-login-id").send_keys("test_id")
driver.find_element_by_id("fm-login-password").clear()
driver.find_element_by_id("fm-login-password").send_keys("test_pass")
driver.find_element_by_id("fm-login-submit").click()`
我试图在Selenium IDE的帮助下做到这一点,并且一切都在GUI中工作.但是在我将代码导出到python并运行它之后,程序给出了一个错误,即找不到该元素.
I tried to do this with the help of Selenium IDE, and everything worked in the GUI. But after I exported the code to python and ran it, the program gave an error that it could not find the element.
推荐答案
但是,所需的元素在<iframe>
中,因此您必须:
However as the the desired elements are within an <iframe>
so you have to:
- 诱导 WebDriverWait 以使所需的框架可用并切换到.
- 诱导 WebDriverWait 以使所需的元素可点击.
-
您可以使用以下解决方案:
- Induce WebDriverWait for the desired frame to be available and switch to it.
- Induce WebDriverWait for the desired elements to be clickable.
You can use the following solution:
-
使用
CSS_SELECTOR
:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get("https://login.aliexpress.com/")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#alibaba-login-box[src^='https://passport.aliexpress.com/mini_login.htm?']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.fm-text#fm-login-id"))).send_keys("test_id")
driver.find_element_by_css_selector("input.fm-text#fm-login-password").send_keys("test_pass")
driver.find_element_by_css_selector("input.fm-button#fm-login-submit").click()
临时浏览快照:
-
注意:您必须添加以下导入:
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
您可以在
这篇关于硒找不到元素,但是元素在https://login.aliexpress.com/网页上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!