Password disappears when sent by selenium - python

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()

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()

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()')

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.

Related

Script will not locate Twitter's password field

In this super basic python script using Selenium, I am just trying to automate my twitter login so I can begin scraping. When the chrome session opens, the username is filled out, but the password field is left blank.
import bs4
from selenium import webdriver
driver = webdriver.Chrome();
url = "https://twitter.com/login"
driver.get(url)
assert "Twitter" in driver.title
username = driver.find_element_by_class_name('js-username-field')
username.send_keys('example_username')
password = driver.find_element_by_class_name('js-password-field')
password.clear()
password.send_keys('exmaple_password')
login_button = driver.find_element_by_css_selector("button.submit.EdgeButton.EdgeButton--primary.EdgeButtom--medium")
login_button.submit()
It's possible you need to add a wait on the password field. This can help if you are seeing intermittent issues with your script. I prefer to wait on an element rather than sleep.
import bs4
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.Chrome();
url = "https://twitter.com/login"
driver.get(url)
assert "Twitter" in driver.title
username = driver.find_element_by_class_name('js-username-field')
username.send_keys('example_username')
# wait on password field
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'js-password-field')))
password = driver.find_element_by_class_name('js-password-field')
password.clear()
password.send_keys('exmaple_password')
login_button = driver.find_element_by_css_selector("button.submit.EdgeButton.EdgeButton--primary.EdgeButtom--medium")
login_button.submit()
Python moves through steps pretty quickly, and it takes a while for web pages to catch up sometimes. Adding a wait helps slow things down a bit, and can fix some intermittent errors you might be seeing.

Passing variables from one Python file to another?

So basically i made a little selenium script and i want to save the pass word and email in a different file for easy editing.
For some reason passing the variables is not working and i cant make heads and tails. i get this error:
unused import statement(when i hover over the import line from Details import *)
I still struggle a bit with Python and would love if you dont bash me for a probably obvious mistake :P
Edit: the programm itsself works fine its just when i try to pass variables that it messes up.
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
from Details import *
driver = webdriver.Firefox()
url = "https://discordapp.com/channels/530588470905929729/538868623981412362"
driver.get(url)
email = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"//input[#type='email']")))
email.send_keys(email)
password = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"//input[#type='password']")))
password.send_keys(password + Keys.ENTER)
sleep(5)
textbox = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"//textarea[#placeholder='Message #bot-commands']")))
textbox.send_keys("!work" + Keys.ENTER)
sleep(30)
driver.quit()
this is Details.py :
password = "Password"
email = "Email#email.com"
You use:
from Details import *
And Details contains a variable password. But your other code also has a different variable password.
Try:
import Details
imported_password = Details.password

Selenium - Edit Code using a userinterface

So i automated the task of login into a discordserver and posting a text using Selenium. All works fine but my goal is to make an inputbox for the email and password. The user inputs the information and presses save and the password and mail is supposed to write itsself into the code and save.
What are the best approaches for this? I'm still pretty new to python so go easy on me.
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
driver = webdriver.Firefox()
url = "https://discordapp.com/channels/530588470905929729/538868623981412362"
driver.get(url)
email = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"//input[#type='email']")))
email.send_keys("Mail#mail.com")
password = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"//input[#type='password']")))
password.send_keys("Password" + Keys.ENTER)
sleep(5)
textbox = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"//textarea[#placeholder='Message #bot-commands']")))
textbox.send_keys("!work" + Keys.ENTER)
sleep(30)
driver.quit()

Selenium webdriver stops with no error-message when navigating through paypal login

I'm trying to log into paypal.com and make a payment automatically.
The program successfully loads the login page(https://www.paypal.com/us/signin) and inputs the email, but when I click the next button the web driver unexpectedly closes without generating an error message.
Has anyone encountered this issue before? Could it be because the next button is a disguised captcha, to keep robots from logging in?
I have already tried using time.sleep(3) to give the page time to load. I can't see any other issues with the code.
import time
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
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
def paypal_pay(): # pass in user address
driver = webdriver.Chrome()
timeout = 20
paypal = "https://www.paypal.com/us/signin"
driver.get(paypal)
email = "emailstuff#gmail.com"
emailElement = driver.find_element_by_id('email')
print(emailElement)
emailElement.send_keys(email)
time.sleep(3)
nextElement = driver.find_element_by_id('btnNext').click()
def main():
paypal_pay()
main()
Your code is working fine but the way that you have implemented is causing the problem, I mean you are using the main() method and what it will do is, once this method gets called and executed, it will close all the connections at end. Hence the reason your browser also getting closed without any error because your code is working fine till there:
Try the below modified code without main method which works completely fine :
import time
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
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome("chromedriver.exe");
paypal = "https://www.paypal.com/us/signin"
driver.get(paypal)
email = "hello#gmail.com"
emailElement = driver.find_element_by_id('email')
print(emailElement)
emailElement.send_keys(email)
time.sleep(3)
nextElement = driver.find_element_by_id('btnNext').click()
print("=> Done...")
For more info on main(), refer this link
I hope it helps...
When I run your code After click on next button the Chrome browser crashes and in console i can see the following error.
<selenium.webdriver.remote.webelement.WebElement (session="577ff51b46a27eefeda43ccd320db48b", element="0.571535141628553-1")>
That means you need to start a RemoteWebDriver instead of ChromeDriver.
Step 1: Download the Selenium Standalone Server from following link
https://www.seleniumhq.org/download/
Step 2: open command prompt as an administrator go to the downloaded path and type the below command and press enter
java -jar selenium-server-standalone-3.141.59.jar
Step 3: To verify Hub is running, open a browser and type the below url.Default port of hub is 4444
http://localhost:4444/grid/console
Step 4: Use the following code.If you follow above steps properly it should work perfectly the below code.
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
def paypal_pay(): # pass in user address
desired_caps = DesiredCapabilities.CHROME
grid_url = "http://localhost:4444/wd/hub"
driver = webdriver.Remote(desired_capabilities=desired_caps, command_executor=grid_url)
paypal = "https://www.paypal.com/us/signin"
driver.get(paypal)
email = "emailstuff#gmail.com"
emailElement = driver.find_element_by_id('email')
print(emailElement)
emailElement.send_keys(email)
nextElement = driver.find_element_by_id('btnNext')
nextElement.click()
def main():
paypal_pay()
main()
Please let me know if this work for you.Good Luck.

Empty iframe in Selenium yet filled in browser (python)

I'm trying to control Spotify's browser player. All of the controls are put inside iframe sections.
Problem: The iframes are EMPTY in the Selenium WebDriver object. Yet, the iframes are filled with the correct content in the ACTUAL browser.
Code sample:
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
import time
username = 'myusername'
base_url = 'https://play.spotify.com/user/spotifydiscover/playlist/76HML2OQXigkyKopqYkeng'
browser = None
def login():
global browser
password = input("Password: ")
browser = webdriver.Firefox()
browser.get(base_url)
browser.find_element_by_id('has-account').click()
browser.find_element_by_id('login-usr').clear()
browser.find_element_by_id('login-usr').send_keys(username)
browser.find_element_by_id('login-pass').clear()
browser.find_element_by_id('login-pass').send_keys(password)
browser.find_element_by_id('login-pass').submit()
def next_track():
global browser
wrapper = browser.find_element_by_id("section-collection")
print(wrapper.get_attribute('innerHTML').encode('utf-8'))
iframe = wrapper.find_element_by_tag_name('iframe')
print(iframe.get_attribute('innerHTML').encode('utf-8'))
sub = browser.switch_to_frame(iframe)
sub.find_element_by_id('next').click()
def test():
login()
time.sleep(14) # Adjusted until page is 100% fully loaded
next_track()
time.sleep(40)
The problem is here:
iframe = wrapper.find_element_by_tag_name('iframe')
There are multiple iframes on the page and you are interested in the one with app-player id:
browser.switch_to_frame("app-player")
# now use browser.find_element_* to locate elements inside iframe
Also note that using time.sleep makes your automation code seriously fragile and usually slower than needed - instead use Explicit Waits to wait for the specific conditions to be met on a page.

Categories

Resources