Checksum321
Checksum321

Reputation: 21

How to send text to the Password field using Selenium and Python

Selenium with Chromedriver Python: Unable to locate element: {"method":"cs s selector","selector":"[id="password"]"}

I edited the question to says, it works well when using the chrome browser, but the same code fails in --headless mode (which is what I want to use)

I have been scratching my head over the past week with this issue: Chromedriver cannot find the presence_of_element_located((By.ID, 'password')), although it is clearly there as seen in the screenshot. It finds the username by the same By.ID and inputs it correctly, but when it gets to thew password, it fails.

[****@mtlab-auto tmp]$ google-chrome --version

Google Chrome 83.0.4103.106

headless chrome=83.0.4103.106

Python 3.8.0 (default, May 7 2020, 02:49:39)

    ########### Chrome option  ###############
chromeOptions = webdriver.ChromeOptions()

chromeOptions.add_argument('--headless')
chromeOptions.add_argument('window-size=1920,1080')
chromeOptions.add_argument('start-maximized')
chromeOptions.add_argument('disable-infobars')

prefs = {'download.default_directory' : f'{temp_folder}'}
chromeOptions.add_experimental_option('prefs',prefs)

# capabilities = DesiredCapabilities.CHROME.copy()
# capabilities['acceptSslCerts'] = True 
# capabilities['acceptInsecureCerts'] = True

############# Set chrome proxy ###########
Chrome_Proxy = 'http://135.28.13.11:8888'
#Chrome_Proxy = 'http://autoproxy.sbc.com/autoproxy.cgi'
chromeOptions.add_argument('--proxy-server=%s' % Chrome_Proxy)

############### Enable chrome browser wedriver ##############
browser = webdriver.Chrome(executable_path='/home/dev/chromedriver', options=chromeOptions,\
service_args = [f'--log-path={chromeDriver_log}'])

# browser = webdriver.Chrome(executable_path='/home/dev/chromedriver', options=chromeOptions,\
# desired_capabilities = capabilities, service_args = [f'--log-path={chromeDriver_log}'])

############## Set the timeout values ###############
ignored_exceptions = (NoSuchElementException,StaleElementReferenceException)
#browser.set_page_load_timeout(15)
#browser.implicitly_wait(40)
element_timeout = 30
print()
print(Fore.BLUE + '>>> Accessing Cisco Cloud...')

browser.get(r'https://slexui.cloudapps.cisco.com/SWIFT/LicensingUI/Quickstart')

time.sleep(20)
browser.get_screenshot_as_file('/tmp/main-page.png')

try:
    element_login = EC.presence_of_element_located((By.ID, 'login-button'))
    WebDriverWait(browser, element_timeout).until(element_login)

except TimeoutException:
    print(Fore.RED + 'Fail to load login page')
    print('RC=101,"Error: Failed to load Cisco Website"')
    browser.close()
    quit()

print()
print(browser.title)
print()

######### Login to cisco cloud account ###########
print(Fore.BLUE + ' >>> Login to Cisco Cloud')

######################## Username ##################
try:
    element_username = EC.presence_of_element_located((By.ID, 'userInput'))
    WebDriverWait(browser, element_timeout).until(element_username).send_keys(f'{cisco_userid}')

except TimeoutException:
    print(Fore.RED + 'Fail to load login page')
    print('RC=101,"Error: Failed to login to Cisco Website"')
    browser.close()
    quit()

time.sleep(2)
#browser.find_element_by_id('userInput').send_keys(f'{cisco_userid}')
print('>>>> Username input')
browser.find_element_by_id('login-button').click()

time.sleep(20)
browser.get_screenshot_as_file('/tmp/password.png')

###################### Password ##################
try:
    element_password = EC.presence_of_element_located((By.ID, 'password'))
    WebDriverWait(browser, element_timeout).until(element_password).send_keys(f'{cisco_password}')

except TimeoutException:
    print(Fore.RED + 'Timed out waiting for page to load')
    print('RC=101,"Error: Failed to login to Cisco Website"')
    browser.close()
    quit()

time.sleep(5)
#browser.find_element_by_id('password').send_keys(f'{cisco_password}')
print('>>>> Password input')
browser.find_element_by_id('kc-login').click()

############# Check if we are logged in #############

And here is the log: it finds my username and puts in the correct username, but fails at the password field. I took a screenshot right after I enter my username, and you can clearly see the password field and nothing is found by chromedriver

[1592922143.110][INFO]: [cb94f02b714bfe0ce21b3dac35546605] COMMAND Navigate {
   "url": "https://slexui.cloudapps.cisco.com/SWIFT/LicensingUI/Quickstart"
}
[1592922143.110][INFO]: Waiting for pending navigations...
[1592922143.111][INFO]: Done waiting for pending navigations. Status: ok
[1592922168.003][INFO]: Waiting for pending navigations...
[1592922243.956][INFO]: Done waiting for pending navigations. Status: ok
[1592922243.956][INFO]: [cb94f02b714bfe0ce21b3dac35546605] RESPONSE Navigate
[1592922263.980][INFO]: [cb94f02b714bfe0ce21b3dac35546605] COMMAND Screenshot {

}
[1592922263.980][INFO]: Waiting for pending navigations...
[1592922263.982][INFO]: Done waiting for pending navigations. Status: ok
[1592922264.304][INFO]: Waiting for pending navigations...
[1592922264.305][INFO]: Done waiting for pending navigations. Status: ok
[1592922264.305][INFO]: [cb94f02b714bfe0ce21b3dac35546605] RESPONSE Screenshot "iVBORw0KGgoAAAANSUhEUgAAB4AAAAQ4CAYAAADo08FDAAAAAXNSR0IArs4c6Q
AAIABJREFUeJzs3XecnWWZN/DfmTnTMsmkzARCEpDQErq0QABBREEhSHEtKJaAq7irK7Iq+y7i6loWQRfbWt5XkSILWEFBikgRCIjSCaFICAgJ6clMMpPp7x+DE0ISQpKBgYfv9..."
[1592922264.309][INFO]: [cb94f02b714bfe0ce21b3dac35546605] COMMAND FindElement {
   "using": "css selector",
   "value": "[id=\"login-button\"]"
}
[1592922264.309][INFO]: Waiting for pending navigations...
[1592922264.310][INFO]: Done waiting for pending navigations. Status: ok
[1592922264.329][INFO]: Waiting for pending navigations...
[1592922264.330][INFO]: Done waiting for pending navigations. Status: ok
[1592922264.330][INFO]: [cb94f02b714bfe0ce21b3dac35546605] RESPONSE FindElement {
   "element-6066-11e4-a52e-4f735466cecf": "0db6e420-49df-46ab-9939-b2f471f9d919"
}
[1592922264.331][INFO]: [cb94f02b714bfe0ce21b3dac35546605] COMMAND GetTitle {

}
[1592922264.331][INFO]: Waiting for pending navigations...
[1592922264.332][INFO]: Done waiting for pending navigations. Status: ok
[1592922264.335][INFO]: Waiting for pending navigations...
[1592922264.336][INFO]: Done waiting for pending navigations. Status: ok
[1592922264.336][INFO]: [cb94f02b714bfe0ce21b3dac35546605] RESPONSE GetTitle "Cisco.com Login Page"
[1592922264.337][INFO]: [cb94f02b714bfe0ce21b3dac35546605] COMMAND FindElement {
   "using": "css selector",
   "value": "[id=\"userInput\"]"
}
[1592922264.337][INFO]: Waiting for pending navigations...
[1592922264.338][INFO]: Done waiting for pending navigations. Status: ok
[1592922264.355][INFO]: Waiting for pending navigations...
[1592922264.356][INFO]: Done waiting for pending navigations. Status: ok
[1592922264.356][INFO]: [cb94f02b714bfe0ce21b3dac35546605] RESPONSE FindElement {
   "element-6066-11e4-a52e-4f735466cecf": "72de3929-e23d-436c-ab22-adb6e79109bd"
}
[1592922264.357][INFO]: [cb94f02b714bfe0ce21b3dac35546605] COMMAND TypeElement {
   "id": "72de3929-e23d-436c-ab22-adb6e79109bd",
   "text": "an144a001",
   "value": [ "*", "*", "*", "*", "*", "*", "0", "0", "1" ]
}
[1592922264.357][INFO]: Waiting for pending navigations...
[1592922264.358][INFO]: Done waiting for pending navigations. Status: ok
[1592922264.496][INFO]: Waiting for pending navigations...
[1592922264.497][INFO]: Done waiting for pending navigations. Status: ok
[1592922264.497][INFO]: [cb94f02b714bfe0ce21b3dac35546605] RESPONSE TypeElement
[1592922266.502][INFO]: [cb94f02b714bfe0ce21b3dac35546605] COMMAND FindElement {
   "using": "css selector",
   "value": "[id=\"login-button\"]"
}
[1592922266.502][INFO]: Waiting for pending navigations...
[1592922266.504][INFO]: Done waiting for pending navigations. Status: ok
[1592922266.517][INFO]: Waiting for pending navigations...
[1592922266.518][INFO]: Done waiting for pending navigations. Status: ok
[1592922266.518][INFO]: [cb94f02b714bfe0ce21b3dac35546605] RESPONSE FindElement {
   "element-6066-11e4-a52e-4f735466cecf": "0db6e420-49df-46ab-9939-b2f471f9d919"
}
[1592922266.521][INFO]: [cb94f02b714bfe0ce21b3dac35546605] COMMAND ClickElement {
   "id": "0db6e420-49df-46ab-9939-b2f471f9d919"
}
[1592922266.521][INFO]: Waiting for pending navigations...
[1592922266.522][INFO]: Done waiting for pending navigations. Status: ok
[1592922266.622][INFO]: Waiting for pending navigations...
[1592922266.624][INFO]: Done waiting for pending navigations. Status: ok
[1592922266.624][INFO]: [cb94f02b714bfe0ce21b3dac35546605] RESPONSE ClickElement
[1592922286.634][INFO]: [cb94f02b714bfe0ce21b3dac35546605] COMMAND Screenshot {

}
[1592922286.634][INFO]: Waiting for pending navigations...
[1592922286.636][INFO]: Done waiting for pending navigations. Status: ok
[1592922287.079][INFO]: Waiting for pending navigations...
[1592922287.080][INFO]: Done waiting for pending navigations. Status: ok
[1592922287.080][INFO]: [cb94f02b714bfe0ce21b3dac35546605] RESPONSE Screenshot "iVBORw0KGgoAAAANSUhEUgAAB4AAAAQ4CAYAAADo08FDAAAAAXNSR0IArs4c6Q
AAIABJREFUeJzs3XmYnFWdL/BvVVd3p7uTzk72QAiBQFiCsqNsAVzhDoIoijqIo86AOnp1HK+z4DIzOug4V8dt0FHwKqCAsiggKosgEER22RMEAiEL2Tvpte4fkQ6dfemk4OXze..."
[1592922287.087][INFO]: [cb94f02b714bfe0ce21b3dac35546605] COMMAND FindElement {
   "using": "css selector",
   "value": "[name=\"password\"]"
}
[1592922287.087][INFO]: Waiting for pending navigations...
[1592922287.088][INFO]: Done waiting for pending navigations. Status: ok
[1592922287.109][INFO]: Waiting for pending navigations...
[1592922287.110][INFO]: Done waiting for pending navigations. Status: ok
[1592922287.110][INFO]: [cb94f02b714bfe0ce21b3dac35546605] RESPONSE FindElement ERROR no such element: Unable to locate element: {"method":"cs
s selector","selector":"[name="password"]"}
  (Session info: headless chrome=83.0.4103.106)
[1592922287.614][INFO]: [cb94f02b714bfe0ce21b3dac35546605] COMMAND FindElement {
   "using": "css selector",
   "value": "[name=\"password\"]"
}
[1592922287.614][INFO]: Waiting for pending navigations...
[1592922287.615][INFO]: Done waiting for pending navigations. Status: ok
[1592922287.628][INFO]: Waiting for pending navigations...
[1592922287.629][INFO]: Done waiting for pending navigations. Status: ok
[1592922287.629][INFO]: [cb94f02b714bfe0ce21b3dac35546605] RESPONSE FindElement ERROR no such element: Unable to locate element: {"method":"cs
s selector","selector":"[name="password"]"}
  (Session info: headless chrome=83.0.4103.106)
[1592922288.132][INFO]: [cb94f02b714bfe0ce21b3dac35546605] COMMAND FindElement {
   "using": "css selector",
   "value": "[name=\"password\"]"
}
[1592922288.132][INFO]: Waiting for pending navigations...
[1592922288.133][INFO]: Done waiting for pending navigations. Status: ok
[1592922288.145][INFO]: Waiting for pending navigations...
[1592922288.146][INFO]: Done waiting for pending navigations. Status: ok
[1592922288.146][INFO]: [cb94f02b714bfe0ce21b3dac35546605] RESPONSE FindElement ERROR no such element: Unable to locate element: {"method":"cs
s selector","selector":"[name="password"]"}
  (Session info: headless chrome=83.0.4103.106)
[1592922288.649][INFO]: [cb94f02b714bfe0ce21b3dac35546605] COMMAND FindElement {
   "using": "css selector",
   "value": "[name=\"password\"]"
}
[1592922288.649][INFO]: Waiting for pending navigations...
[1592922288.650][INFO]: Done waiting for pending navigations. Status: ok
[1592922288.662][INFO]: Waiting for pending navigations...
[1592922288.663][INFO]: Done waiting for pending navigations. Status: ok
[1592922288.663][INFO]: [cb94f02b714bfe0ce21b3dac35546605] RESPONSE FindElement ERROR no such element: Unable to locate element: {"method":"cs
s selector","selector":"[name="password"]"}
  (Session info: headless chrome=83.0.4103.106)
[1592922289.166][INFO]: [cb94f02b714bfe0ce21b3dac35546605] COMMAND FindElement {
   "using": "css selector",
   "value": "[name=\"password\"]"
}

enter image description here

enter image description here

Upvotes: 0

Views: 2004

Answers (2)

Ischio
Ischio

Reputation: 26

Some problems i was facing in your some situation:

  • The html of the page had been loading differently using the headless mode and it didn't find some elements. Try using it commenting headless mode.
  • Try finding it in a different way, i.e. using find_element_by_xpath or class_name.
  • Try using interactive console, get the full path of the password element in the browser windows loaded by selenium and execute the command `find_element_by_xpath(full_path). It should work, if not try interactivly all methods and then change accordingly in your code.

Hope to be usefull

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193108

To send a character sequence within the Password field you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    driver.get('https://slexui.cloudapps.cisco.com/SWIFT/LicensingUI/Quickstart')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#userInput"))).send_keys("an144a001")
    driver.find_element_by_css_selector("input#login-button").click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#password"))).send_keys("Checksum321")
    
  • Using XPATH:

    driver.get('https://slexui.cloudapps.cisco.com/SWIFT/LicensingUI/Quickstart')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='userInput']"))).send_keys("an144a001")
    driver.find_element_by_xpath("//input[@id='login-button']").click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='password']"))).send_keys("Checksum321")
    
  • 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
    
  • Browser Snapshot:

identity_cisco

Upvotes: 1

Related Questions