Passing variables from one Python file to another? - python

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

Related

Using Selenium Python generate a new password [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I forgot my password and I wrote this code with Selenium to try to recover my pass, but I don't know how to continue.
The button new password doesn't work.
Can someone help me? I try different ways to continue but I don't know. Is any way to enter?
I am trying to remember my password using the function. Something like force brute. So, I dont know how to make a loop generating a new password, with the function, enter it in the box "Contraseña: " until it is correct
from selenium.webdriver.common.keys import Keys
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.webdriver.support.ui import Select
import time
import os
import datetime
from datetime import datetime
from playsound import playsound
import smtplib, ssl
from random import sample
def password_generator(longitud):
abc_minusculas = "abcdefghijklmnopqrstuvwxyz"
abc_mayusculas = abc_minusculas.upper()
numeros = "0123456789"
secuencia = abc_minusculas + abc_mayusculas + numeros
password_union = sample(secuencia, longitud)
password_result = "".join(password_union)
return password_result
PATH = "C:\Program Files (x86)\Chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("http://siga.frba.utn.edu.ar/")
print(driver.title)
link = driver.find_element_by_id("page-try")
link.click()
time.sleep(1)
link = driver.find_element_by_xpath('//*[#id="menu-page-try"]/p[4]/a')
link.click()
time.sleep(1)
search = driver.find_element_by_name("form_email")
search.send_keys("35335")
search.send_keys(Keys.RETURN)
time.sleep(0.5)
1 The locator you used is not unique (there are two of them). So I replaced it with xpath locator //input[#name='form_email'].
2 You are using time.sleep everywhere. It may be slower then using Selenium's explicit/implicit wait and also it's very unstable
So, I added wait = WebDriverWait(driver, 30) to your code and applied explicit waits.
3 Also, I noticed that page-try id is not unique, so I replaced it with css selector. If you are using find_element..., not find_elements... then your locators have to be unique.
Solution
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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.webdriver.support.ui import Select
import time
import os
import datetime
from datetime import datetime
import smtplib, ssl
from random import sample
PATH = '/snap/bin/chromium.chromedriver'
driver = webdriver.Chrome(executable_path=PATH)
driver.get("http://siga.frba.utn.edu.ar/")
print(driver.title)
wait = WebDriverWait(driver, 30)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#page-try"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(text(), 'Usuarios registrados')]"))).click()
search = wait.until(EC.element_to_be_clickable((By.XPATH, "//input[#name='form_email']")))
search.send_keys("35335")
search.send_keys(Keys.RETURN)
To click Solicitar nueva contraseña button use this xpath: //a[contains(text(), 'Solicitar nueva contraseña')]
Try using the locators I provided as examples for other elements on your site. Use this as a reference https://www.w3schools.com/xml/xpath_intro.asp

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

Python converted to .exe but the user needs to be able to edit files

I made a script which automates some processes using Selenium and another Script where I store variables like password and email.
I converted it to an a .exe file but the user needs to be able to edit the Details.py file.
Is there any work around since as soon as I convert it to .exe theres no way for me to edit and save files?
Edit: Posted code to help with an answer:
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 = "mail#mail.com"
You need to rethink the design of your program. It's not secure or user-friendly to edit .py files in order to alter saved credentials. I suggest you have a look around for already proven methods of storing sensitive information. There are lots of options out there and you can start with this post.

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.

Categories

Resources