1

I cannot locate the username and password fields. I inspect the elements, and tried finding it by id, xpath or css selector, but it gives me error NoSuchElementException: Message: no such element: Unable to locate element.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By 
from selenium.webdriver.common.action_chains import ActionChains
import time 



if __name__ == "__main__":
    option = webdriver.ChromeOptions()
    option.add_argument("--incognito")
    option.add_argument("--start-maximized")

    if getattr(sys, 'frozen', False):
        chromedriver_path = os.path.join(sys._MEIPASS, "chromedriver.exe")
        driver = webdriver.Chrome(chromedriver_path, options=option)
    else:
        driver = webdriver.Chrome(options=option)

    driver.get("https://www.wix.com/")
    loginPage = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/header/nav/a[2]"))).click()

    usernameField = driver.find_element_by_id("input_4")
    passwordField = driver.find_element_by_xpath("input_5")

    usernameField.send_keys("user")
    passwordField.send_keys("pass")
    time.sleep(5)
    driver.quit()

The error I get is:

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"input_4"}
  (Session info: chrome=71.0.3578.98)
  (Driver info: chromedriver=2.44.609538 (b655c5a60b0b544917107a59d4153d4bf78e1b90),platform=Windows NT 10.0.17134 x86_64)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
paul
  • 121
  • 1
  • 10

2 Answers2

1

If you want to locate email and password fields you can try

usernameField = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.NAME, "email")))
passwordField = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.NAME, "password")))
usernameField.send_keys("user")
passwordField.send_keys("pass")

The reason why you cannot locate fields:

  • driver.find_element_by_id("input_4") - I see no elements with id="input_4". @id value might be dynamic.
  • driver.find_element_by_xpath("input_5") - "input_5" is not valid XPath syntax. You might need to use //*[@id="input_5"], but anyway I also see no elements with id="input_4"...
Andersson
  • 51,635
  • 17
  • 77
  • 129
  • Thanks! it works. For me it appeared as "input_4" that is why I used that. And the xpath I was using "//*[@id="input_5"]" I just forgot to change it to id when I post it. But I also want to ask, why do I need to wait? What is the reason? – paul Jan 21 '19 at 22:14
  • @paul , because those input fields are not present in page source so you need to wait until they apper in DOM – Andersson Jan 22 '19 at 06:29
0

The elements i.e. username and password fields are Angular elements and becomes visible, enabled and clickable once you invoke click() on the element with text as Sign In and JavaScript / AjaxCalls are completed. So you have to induce WebDriverWait for the desired element to be clickable and you can use the following solution:

  • Minimal Code Block:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.ui import WebDriverWait
    
    
    options = webdriver.ChromeOptions()
    options.add_argument("start-maximized")
    options.add_argument("--disable-extensions")
    driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("https://www.wix.com/")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Sign In"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='email']"))).send_keys("paul")
    driver.find_element_by_css_selector("input[name='password']").send_keys("paul")
    
  • Browser Snapshot:

wix_login

Note: You can find a relevant detailed discussion in Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome

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