lastly I'm working about a Python project which uses the library "selenium". The project should type in passwords according to the usernames that it has got. My problem is that I don't know how to get data from a text box.
For example, if I want to use the project on "Facebook", at first I have to get the E-mail which the user typed in (kind of like putting it as a string variable), and then I have to type in the correct password (I know how to do that). My main problem is that the command " .text " doesn't work because the E-mail isn't found in any attribute of the textbox, so I can't call it.
Does someone have a solution please?
The current lines are :
driver = webdriver.Chrome('C:\\Users\User\Desktop\chromedriver.exe')
driver.get('https://www.facebook.com/stype=lo&jlou=AfdFutu6bihEQajPctMRwj2ySoaIGAE71YZ0aBZe9FYV4Xd2XuZ3SGth5wernWcF7s4pSvZH5W6f0ed2BfafHrkbupDcW4GDQECBTnhQID1FQ&smuh=4370&lh=Ac_eTqeC_DcDMq9f')
mail = driver.find_element_by_id('email').text
The problem is that the current "mail" variable is an empty string, because the E-mail textbox of Facebook's website is empty as it's waiting for the client to type in his E-mail. I'm trying to get the client's E-mail AFTER he typed it in.
Thanks in advance!
You can get value from input field simply as
email_input = driver.find_element_by_xpath('//input[#type="email"]')
print(email_input.get_attribute('value'))
Update
If you want to get text from input after user enter email:
driver = webdriver.Chrome('C:\\Users\User\Desktop\chromedriver.exe')
driver.get('https://www.facebook.com/stype=lo&jlou=AfdFutu6bihEQajPctMRwj2ySoaIGAE71YZ0aBZe9FYV4Xd2XuZ3SGth5wernWcF7s4pSvZH5W6f0ed2BfafHrkbupDcW4GDQECBTnhQID1FQ&smuh=4370&lh=Ac_eTqeC_DcDMq9f')
email = input("Please enter your Email: ")
email_input = driver.find_element_by_xpath('//input[#type="email"]')
email_input.send_keys(email)
print(email_input.get_attribute('value')) # or just print(email)
Note, that user should enter email value to command line, but not to browser input field
Related
I am reading .txt file in Python code and I should get the same mail body what I have in my text file.
It is working fine but hyperlinks not displayed in my outlook email, it displays only as text in outlook email.
Below is the code:
Mail_Content = open("MailBody.txt","r")
Read_Content = Mail_Content.read()
In the text file , passing content like this for hyperlink:
linkname,'html'
Please help me out, I am trying to fix this from last two days.
Firstly, you really need to show the code that sets the message body. Secondly, make sure you set the MailItem.HTMLBody rather than the plaintext MailItem.Body.
Make sure the BodyFormat property is set up correctly before setting the HTMLBody property in the code, for example, here is a VBA sample which shows how to set up it properly:
Sub CreateHTMLMail()
'Creates a new email item and modifies its properties.
Dim objMail As MailItem
'Create mail item
Set objMail = Application.CreateItem(olMailItem)
With objMail
'Set body format to HTML
.BodyFormat = olFormatHTML
.HTMLBody = "<HTML><H2>The body of this message will appear in HTML.</H2><BODY>Type the message text here. </BODY></HTML>"
.Display
End With
End Sub
Somehow send_keys enters repeat password or string other than mentioned password leading login failure.
Tried adding explicit wait(), driver.clear() but does not work.
Here is a sample code in Python -
Approach 1 -
driver = webdriver.chrome(executable path)
driver.maximize__window()
driver.get(address)
password = "xyz"
field1 = wait.until(EC.presence_of_element_located(By.XPATH, <xpath of the password field>)
actionChains.move_to_element(field1).click()
actionChains.move_to_element(field1).send_keys(password).perform()
driver.find_element(By.ID, "Login-button").click()
Here instead of "xyz" probably "xyzxyzxyzx" string gets added to the password field(cannot decode as password gets masked).
Please suggest.
Approach 2 -
Also, another try with below code somehow concatenates username to the password while entering password.
username = driver.find_element(By.ID,"USERNAME").click()
actionChains.send_keys("test")
password = driver.find_element(By.ID,"PASSWORD").click()
actionChains.send_keys("xyz")
actionChains.perform()
This snippet results into -
Username as "test"
Password as "testxyz"
Expected output is:
Username as "test"
Password as "xyz"
Sometimes it auto fills the last input. I get around it by just adding "Keys.BACK_SPACE*20" into the send keys brackets
.send_keys(Keys.BACK_SPACE*20, "xyz")
you will also need to import the Keys library:
from selenium.webdriver.common.keys import Keys
Some assumptions that come to the top of my mind are some default values may have been populated already in the password field. This link may be helpful if that's the scenario. I'm not sure if that's the same thing driver.clear() does.
Also if there's a show password option that can be utilised to debug the issue. also other options for those can be gettext from the text box also can be helpful.
I am trying to authenticate proxy with username and password in Selenium using Python but the current code is not working. I have tried many solutions but none of them worked.
Proxy example,
IP = xxx.xx.xx.xx
PORT = xxxxx
USERNAME = USERNAME
PASSWORD = PASSWORD
I have used the following code,
driver.execute_script("""
Services.prefs.setIntPref('network.proxy.type', 1);
Services.prefs.setCharPref("network.proxy.http", arguments[0]);
Services.prefs.setIntPref("network.proxy.http_port", arguments[1]);
Services.prefs.setCharPref("network.proxy.ssl", arguments[0]);
Services.prefs.setIntPref("network.proxy.ssl_port", arguments[1]);
Services.prefs.setCharPref('network.proxy.socks', arguments[4]);
Services.prefs.setIntPref('network.proxy.socks_port', arguments[5]);
Services.prefs.setCharPref('network.proxy.socks_username', arguments[6]);
Services.prefs.setCharPref('network.proxy.socks_password', arguments[7]);
""", http_addr, http_port, ssl_addr, ssl_port, socks_addr, socks_port, socks_username, socks_password)
I have tried some other code snippets also. I tried to place values into alert boxes also.
You can achieve this by using AutoIt. And it has Python binding PyAutoIt. Once you installed PyAutoIt using PIP - pip install PyAutoIt, the following code does your job.
import autoit
autoit.win_wait_active("Authentication Required") # title of the dialog box to wait. so it will wait for the Authentication Required dialog
autoit.send("username", 1) # second parameter is the mode (changes how "keys" is processed)
autoit.send("{TAB}") # press tab key to go to the password field
autoit.send("password", 1)
autoit.send("{Enter}") # press enter key
For more information about the second parameter in the send method, here is the code,
def send(send_text, mode=0):
"""
Sends simulated keystrokes to the active window.
:param send_text:
:param mode: Changes how "keys" is processed:
flag = 0 (default), Text contains special characters like + and ! to
indicate SHIFT and ALT key presses.
flag = 1, keys are sent raw.
:return:
"""
AUTO_IT.AU3_Send(LPCWSTR(send_text), INT(mode))
I am attempting to automate a login process.
To do so, I am using send_keys('email') and send_keys('password') in their respective fields like so:
email = driver.find_element_by_xpath('//*[#id="email"]')
email.send_keys('e#mail.com')
pwd = driver.find_element_by_xpath('//*[#id="password"]')
pwd.send_keys('pwd12345')
These two steps are successful. Now, I would like to use the "return" key to submit the login credentials and be logged in. To do so, I have tried:
pwd.send_keys(Keys.RETURN)
The password I am attempting to enter is 8 characters long. After the 8 characters have been entered, pwd.send_keys(Keys.RETURN) enters a 9th character into the password field JUST before the credentials are submitted, resulting in "incorrect email address or password".
I have also tried:
email.send_keys(Keys.RETURN) - which also returns "incorrect email address or password"
pwd.submit() - which clears the email and password fields completely and does not log in (?)
Thoughts?
Can you try code below?
pwd = driver.find_element_by_xpath('//*[#id="password"]')
ActionChains(driver).send_keys_to_element(pwd,'pwd12345').send_keys_to_element(pwd, Keys.RETURN).perform()
Here is the Answer to your Question:
Considering you are trying to login in to your PayPal Account with a valid set of credentials the following code block works through all the 3 options as in:
Using click()
Using Keys.ENTER
Using Keys.RETURN
Having said that I will suggest you to consider using the click() and clear() methods before entering any text into any input tag. Here is the working code block:
driver.get('https://www.paypal.com/in/home');
driver.implicitly_wait(20);
driver.find_element_by_id('ul-btn').click();
email = driver.find_element_by_id('email')
email.click();
email.clear();
email.send_keys('debanjan.selenium#gmail.com');
password = driver.find_element_by_id('password');
password.click();
password.clear();
password.send_keys('dev_anjan');
#Using Keys.ENTER
#password.send_keys(Keys.ENTER);
#Using Keys.RETURN
password.send_keys(Keys.RETURN);
#Using LogIn Button Click
#login_button = driver.find_element_by_id('btnLogin')
#login_button.click();
Let me know if this Answers your Question.
Please try this way, it's working for me:
email = driver.find_element_by_xpath('//*[#id="email"]')
email.send_keys('e#mail.com')
pwd = driver.find_element_by_xpath('//*[#id="password"]')
pwd.send_keys('pwd12345')
login_button = driver.find_element_by_xpath('//*[#id="btnLogin"]')
login_button.click()
Insted of pressing Enter on the password field you should do what a normal guy does and press the Log In button.
I am doing a fun little project, making a kind of secret code program with a GUI(Tkinter) to encrypt my text(not very securely). I am trying to make a password on this program linked to a txt file. The program has default password 'abc', stored in a text file in sha-224. When the user enters 'abc', it will hash their input in sha-224, and compare it to the stored password in _hexutf2.txt. Once they have logged in, the user will have the option to choose a new password by clicking New Passcode, entering their previous passcode, clicking next, and then clicking new code. After clicking new code, the user enters a new passcode in the entrypassVariable, then clicks enter passcode, which will write the new passcode on the txt file. Instead, the program hangs on the first pressing of Enter passcode, despite the fact that I had entered 'abc' the default passcode. The program worked before I added the password element, so I will post only the password code here, but I will link to the entire program if anyone wants to see it.
EDIT
Posting just the essential code here. The problem in my main program is caused by this. For some reason, this program prints this:
Stored Password: cd62248424c8057fea8fff161ec753d7a29f47a7d0af2036a2e79632
Enter Password: Moo
Password Attempt Hash: cd62248424c8057fea8fff161ec753d7a29f47a7d0af2036a2e79632
Password Incorrect
http://snipplr.com/view/75865/cryptographer/ <----Entire program code
import hashlib
passfile = open('pass.txt','r')
stored_password = str(passfile.read())
print 'Stored Password: ' + stored_password
password = raw_input('Enter Password: ')
enter_pass_sha = hashlib.sha224(password)
enter_password = str(enter_pass_sha.hexdigest())
print 'Password Attempt Hash: ' + enter_password
if stored_password == enter_password:
print 'Password Correct'
else:
print 'Password Incorrect'
You should have noticed this:
Stored Password: cd62248424c8057fea8fff161ec753d7a29f47a7d0af2036a2e79632
# blank line?!
Enter Password: Moo
Password Attempt Hash: cd62248424c8057fea8fff161ec753d7a29f47a7d0af2036a2e79632
Password Incorrect
When you read the stored_password in from the passfile, it comes with a newline character '\n' at the end. You need to do:
with open('pass.txt') as passfile:
stored_password = passfile.read().strip()
print 'Stored Password: ' + stored_password
Note str.strip, called without arguments, removes all whitespace, including newlines, from the start and end of the string. Note also the use of the with context manager for file handling, which handles errors and closes the file for you.
One issue is hexCode = passfile.read - that's a bound method, not a string (you never called it with ()). This will not match any of your strings, and causes an exit from OnPassClick or OnNextClick, or a funny message from OnNewClick. Similarly, you raised the class SystemExit type instead of an instance of it.