1

This is just a quick sanity check I'm hoping someone can put some eyes on.

Basically, I'm trying to log into this site (ArcGIS Online) with selenium: https://www.arcgis.com/home/signin.html?useLandingPage=true".

This is what the elements look like: enter image description here

My code for the login looks like this:

user = driver.find_element_by_name("user_username")
password = driver.find_element_by_name("user_password")
user.clear()
user.send_keys(username)
password.clear()
password.send_keys(password)
driver.find_element_by_id("signIn").click()

In this case, I'm using the ArcGIS login option...not sure if I need to be activating that first? I'm assuming there's a step I'm missing here to get this to work.

The error I get currently is:

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="user_username"]"}

Really appreciate any thoughts people have out there!

EDIT:

Updated code to this:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

options = webdriver.ChromeOptions()
options.headless = True
browser = webdriver.Chrome(options=options)
url = "https://www.arcgis.com/home/signin.html?useLandingPage=true"
browser.get(url)

try:
    user = WebDriverWait(browser, 10).until(
        EC.presence_of_element_located((By.ID, "user_username"))
    )

    password = WebDriverWait(browser, 10).until(
        EC.presence_of_element_located((By.ID, "user_password"))
    )  

    signin = WebDriverWait(browser, 10).until(
        EC.presence_of_element_located((By.ID, "signIn"))
    ) 

    user.clear()
    user.send_keys(username)
    password.clear()
    password.send_keys(password)
    signin.click()

finally:
    browser.quit()

And now get this error:

TypeError: object of type 'WebElement' has no len()

Probably doing something wrong, will look deeper into the proper usage of wait functionality.

FINAL EDIT:

For anyone chancing upon this page, the solution was to wait for elements to load, make sure I was searching by IDs, and...make sure I had no typos! Below is a working scenario:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

username = "Hari"
password = "Seldon" 
options = webdriver.ChromeOptions()
options.headless = True
driver = webdriver.Chrome(options=options)
url = "https://www.arcgis.com/home/signin.html?useLandingPage=true"
driver.get(url)
try:
    user = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "user_username")))
    passwd = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "user_password")))
    user.clear()
    user.send_keys(username)
    passwd.clear()
    passwd.send_keys(password)
    driver.find_element_by_id("signIn").click()  

finally:
    driver.quit()
TapeHead
  • 120
  • 7
  • 1
    There seems to be a typo in the id. It's not ser_username. its user_username in the site. – koushikmln May 06 '20 at 14:09
  • My mistake I copied it over incorrectly. It's corrected now, but doesn't run with "user_username" either. – TapeHead May 06 '20 at 14:13
  • 1
    Try this Link: https://stackoverflow.com/questions/27112731/selenium-common-exceptions-nosuchelementexception-message-unable-to-locate-ele – Mayank May 06 '20 at 14:24
  • That's helpful - I can see it does find the element now, but I get the error: "TypeError: object of type 'WebElement' has no len()" – TapeHead May 06 '20 at 14:58

2 Answers2

1

You are using find_element_by_name but using the value of id in the code. I changed it to find_element_by_id and it works. Code below:

user = browser.find_element_by_id("user_username")
password = browser.find_element_by_id("user_password")
user.clear()
user.send_keys(username)
password.clear()
password.send_keys(password)
browser.find_element_by_id("signIn").click()
koushikmln
  • 648
  • 6
  • 23
  • Yep, tried that variation too but still get the exception: NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="user_username"]"} – TapeHead May 06 '20 at 14:54
1

You should wait for the elements to load on the page first:

user = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, "user_username")))
password = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, "user_password")))
user.clear()
user.send_keys("aaa")
password.clear()
password.send_keys("bbb")
driver.find_element_by_id("signIn").click()

You need these imports for the wait functionality:

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
0buz
  • 3,443
  • 2
  • 8
  • 29
  • Thanks - that's pretty much what I have now, but still no luck. It seems I was able to place the username/password but the sign-in click isn't working. – TapeHead May 06 '20 at 15:17
  • It seems to consistently be: "TypeError: object of type 'WebElement' has no len()" – TapeHead May 06 '20 at 15:20
  • I've just seen your edit. You have a variable called "password" which stores the webelement you will send_keys to. But what you are sending to that weblements is a variable having the same name. So you are trying to type in a webelement. Make sure to have different variable names and try again. – 0buz May 06 '20 at 15:25
  • GOOD CATCH that was supppper sloppy of me sorry. Too much multitasking at the moment. I just updated the code and no errors! – TapeHead May 06 '20 at 15:43