Selenium can't find element, but element is on the https://login.aliexpress.com/ webpage

。_饼干妹妹 提交于 2019-12-19 10:31:48

问题


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()`

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.


回答1:


However as the the desired elements are within an <iframe> so you have to:

  • 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:

    • Using 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()
      
  • Interim Broswer Snapshot:

  • 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
    
  • Reference:

    • Here you can find a relevant discussion on Ways to deal with #document under iframe



回答2:


The login form is inside of a frame, you need to switch to it first.

from selenium import webdriver
from time import sleep
driver = webdriver.Firefox() 
driver.get("https://login.aliexpress.com/")

frame = driver.find_element_by_id("alibaba-login-box")
driver.switch_to.frame(frame)

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()


来源:https://stackoverflow.com/questions/53282224/selenium-cant-find-element-but-element-is-on-the-https-login-aliexpress-com

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!