2

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.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 3
    Possible duplicate of [function for switching frames in python, selenium](https://stackoverflow.com/questions/28723143/function-for-switching-frames-in-python-selenium) – Guy Nov 13 '18 at 13:39

2 Answers2

3

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()
Lucas Tierney
  • 2,503
  • 15
  • 13
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:

login_aliexpress_com

  • 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

You can find a relevant discussion in

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352