reggie jones
reggie jones

Reputation: 99

Password disappears when sent by selenium

Ok, To start, Everything here is working. My issue right now is when the password sends the keys it disappears immediately. It's happening because the password input itself erases the password when you click the input again. My question is, is there a workaround to get the password injected without it disappearing?

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from random import randint
import pickle
import datetime
import os
import time

url = 'https://sef.clareityiam.net/idp/login'

current_time = datetime.datetime.now()
current_time.strftime("%m/%d/%Y")

chromedriver = "chromedriver.exe"

driver = webdriver.Chrome(chromedriver)
driver.get(url)
pickle.dump(driver.get_cookies() , open("cookies.pkl","wb"))

user = driver.find_element_by_id("clareity")
user.send_keys("user")

password = driver.find_element_by_id("security")
password.send_keys("Password")


button = driver.find_element_by_id("loginbtn")
button.click()

Upvotes: 1

Views: 612

Answers (3)

vitaliis
vitaliis

Reputation: 4212

Click this field before entering data. This works for me. Also, use at least implicit wait. Another issue in your code may be that the id for password is not unique. There are two such locators.

import pickle
import time
from selenium import webdriver


url = 'https://sef.clareityiam.net/idp/login'
driver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver')
driver.get(url)
driver.implicitly_wait(10)
pickle.dump(driver.get_cookies(), open("cookies.pkl","wb"))
user = driver.find_element_by_id("clareity")
user.send_keys("user")
password = driver.find_element_by_xpath('//div[@data-ph="PASSWORD"]')
password.click()
password.send_keys("Password")
time.sleep(6)  # Added temporary so you could see that password stays
button = driver.find_element_by_id("loginbtn")
button.click()

Upvotes: 2

reggie jones
reggie jones

Reputation: 99

I found somewhat of a work around for it. Idk why i didn't think about this while i was writing this script but.

if you send the passwords send_keys twice it stays. Don't understand why, but it works.

Upvotes: 0

xcapt
xcapt

Reputation: 84

Alternatively, to send keys in the input tag you can use the execute_script method with javascript. For example

driver.execute_script('document.getElementById("security").setAttribute("value", "12345")')

Then you can submit this form by clicking on the button or submit via javascript like this

driver.execute_script('document.querySelector("form").submit()')

Upvotes: 0

Related Questions