I am trying to login into the ESPN footytips website so that I can scrape information for one of my leagues.
I am having no issues opening an instance of Chrome and navigating to the homepage (which contains the login form) and can even select the username field but I cannot for the life of me send my login details to the form.
In debugging I know I can find and select the form submit button and the issue seems to be in passing my login details using send_keys as my exception rule always triggers after I attempt call send_keys.
Any suggestions on how to resolve would be welcomed! My script is below:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
login_address = "http://www.footytips.com.au/home"
me_login = "test#test.com"
me_password = "N0TMYR3#LP#S5W0RD"
browser = webdriver.Chrome()
browser.get(login_address)
try:
login_field = browser.find_element_by_id("ft_username")
password_field = browser.find_element_by_id("ft_password")
print("User login fields found")
login_field.send_keys(me_login)
password_field.send_keys(me_password)
print("Entered login data")
submit_button = browser.find_element_by_id("signin-ft")
print("Submit button found")
submit_button.submit()
except:
print("Error: unable to enter form data")
The locators you have used doesn't uniquely identifies the login_field and the password_field. Additionally you need to wait for the respective WebElements to be visible. Here is your own code with some tweaks :
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
#lines of code
login_address = "http://www.footytips.com.au/home"
me_login = "test#test.com"
me_password = "N0TMYR3#LP#S5W0RD"
browser.get(login_address)
login_field = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,"//div[#class='login-form']//input[#id='ft_username']")))
password_field = browser.find_element_by_xpath("//div[#class='login-form']//input[#id='ft_password']")
login_field.send_keys(me_login)
password_field.send_keys(me_password)
print("Entered login data")
Related
I am trying to log into twitter to access direct messages. And I need to automate the process using selenium. To do this I have to log in to twitter and authorize my app.
driver = webdriver.Chrome(service=s)
# Selenium opens up webpage
driver.get("https://twitter.com/i/flow/login?input_flow_data=%7B%22requested_variant%22%3A%22eyJoaWRlX21lc3NhZ2UiOiJ0cnVlIiwicmVkaXJlY3RfYWZ0ZXJfbG9naW4iOiJodHRwczovL3R3aXR0ZXIuY29tL2kvb2F1dGgyL2F1dGhvcml6ZT9zdGF0ZT12RkpVMVdDVmYxTDNlVlpqQUU0Z1ZWT1hmNmZHSkEmcmVzcG9uc2VfdHlwZT1jb2RlJnJlZGlyZWN0X3VyaT1odHRwcyUzQSUyRiUyRnd3dy5zcG9ydHNkYXRhbm93LmNvbSUyRiZzY29wZT1kbS5yZWFkJTIwdHdlZXQucmVhZCUyMHVzZXJzLnJlYWQlMjBvZmZsaW5lLmFjY2VzcyZjbGllbnRfaWQ9YUVGMVdYWnZhVzB5WlVKTFV6STFTMFV4VGt3Nk1UcGphUSZjb2RlX2NoYWxsZW5nZV9tZXRob2Q9UzI1NiZjb2RlX2NoYWxsZW5nZT1MOW9xTGsySnhWQTU2TG85Z3d2T1JMWWJ4cWVMNks0QkZYTW4weXhnNmxFIn0%3D%22%7D")
username_input = driver.find_element(By.NAME, "text")
username_input.send_keys('send username here')
This code is opening the site and then I am trying to enter the username however I get an error that says no such element is found. Any help would be much appreciated.
May you need a bit wait:
username_input = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, 'text')))
username_input.send_keys('send username here')
Following import:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
I am trying to login Naukri using python and selenium, I was able to access the login button using Xpath from the website https://www.naukri.com/, but I am unable to populate my mail ID in Email ID / Username, in the needed box. code I wrote
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.naukri.com/')
login = driver.find_element_by_xpath('//*[#id="login_Layer"]/div')
login.click()
userName = driver.find_element_by_xpath('//*[#id="root"]/div[2]/div[2]/div/form/div[2]/input')
userName.send_keys('jones1234#gmail.com')
I dont know, if I have the correct Xpath or not, my maild ID is not getting populated in. I think I have the incorrect Xpath, but I am not sure kindly help.
Add some wait prior to finding the element. You need to allow the element to popup after clicking the login.
wait = WebDriverWait(driver, 10)
driver.get('https://www.naukri.com/')
login = driver.find_element_by_xpath('//*[#id="login_Layer"]/div')
login.click()
userName=wait.until(EC.element_to_be_clickable((By.XPATH,'//*[#id="root"]/div[2]/div[2]/div/form/div[2]/input')))
userName.send_keys('jones1234#gmail.com')
Import
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
I am trying to automate posting into my social media account. I have successfully written the script to log in, after login in I am having difficulty locating the textarea element with which to pass my post, after which I will try to attach an image to my post, make the post and log out. But for now, I am stuck at the locating the textarea after login in. This is the code
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
usernameStr = 'JonJames3872#gmail.com'
passwordStr = 'JamesJon'
textStr = 'Testing my Post'
browser = webdriver.Chrome()
browser.get(('https://accounts.kingsch.at/?client_id=com.kingschat&scopes=%5B%22kingschat%22%5D&redirect_uri=https%3A%2F%2Fweb.kingsch.at%2F'))
# fill in username and password
password = browser.find_element_by_name('password')
password.send_keys(passwordStr)
username = browser.find_element_by_class_name('field')
username.send_keys(usernameStr)
signInButton = browser.find_element_by_class_name('submit-btn')
signInButton.click()
# I HAVE LOGGED IN, NOW THIS IS WHERE MY CODE HAS A PROBLEM
text = browser.find_element_by_xpath("/html/body/div[1]/div/div[2]/div/div[2]/div/div[1]/div/div/div[1]/textarea")
text.send_keys(textStr)
This is the element, from inspect element:
<textarea placeholder="What's happening?" class="KingingBox__input"></textarea>
Textarea HTML Screenshot
URL: https://accounts.kingsch.at/?client_id=com.kingschat&scopes=%5B%22kingschat%22%5D&redirect_uri=https%3A%2F%2Fweb.kingsch.at%2F
Sample User name: JonJames3872#gmail.com (strangely here username is case sensitive)
Sample password: JamesJon
Notification Screenshot
I inserted code to write something in the text area
I inserted code to upload a photo
I inserted code to post your own post
I think 3 out of the 3 questions are all done.
Try this code:
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
import os
path = '/home/avionerman/Documents/stack'
browser = webdriver.Firefox(path)
browser.implicitly_wait(10)
usernameStr = 'JonJames3872#gmail.com'
passwordStr = 'JamesJon'
textStr = 'Testing my Post'
browser.get(('https://accounts.kingsch.at/?client_id=com.kingschat&scopes=%5B%22kingschat%22%5D&redirect_uri=https%3A%2F%2Fweb.kingsch.at%2F'))
# fill in username and password
password = browser.find_element_by_name('password')
password.send_keys(passwordStr)
username = browser.find_element_by_class_name('field')
username.send_keys(usernameStr)
signInButton = browser.find_element_by_class_name('submit-btn')
signInButton.click()
text = browser.find_element_by_class_name('KingingBox__input')
text.clear()
text.send_keys(textStr)
time.sleep(5)
image = browser.find_element_by_class_name('KingingBox__attachment-input').send_keys('/here/path/of_yours/th_574e7c36606306d94a4.jpg')
time.sleep(5)
inserted_photo = browser.find_element_by_class_name('KingingBox__attachments-list')
if inserted_photo.is_displayed():
print("Element found, photo uploaded successfully")
browser.find_element_by_css_selector('.KingingBox__submit-btn').click()
else:
print("Element not found")
At this line:
browser.implicitly_wait(10)
we determine that the browser will wait a max time of 10 seconds for each element to be visible. If the textarea will make more than 10 seconds to appear, the the script will stop. If you see enormous delays increase the seconds of waiting.
Also, as you can see I used this line:
text = browser.find_element_by_class_name('KingingBox__input')
in order to locate the textarea.
In this line:
image = browser.find_element_by_class_name('KingingBox__attachment-input').send_keys('/here/path/of_yours/th_574e7c36606306d94a4.jpg')
I locate the input tag which is responsible for accepting uploades and then I send on it the exact path of the file that I want to upload.
In the last part:
inserted_photo = browser.find_element_by_class_name('KingingBox__attachments-list')
if inserted_photo.is_displayed():
print("Element found, photo uploaded successfully")
browser.find_element_by_css_selector('.KingingBox__submit-btn').click()
else:
print("Element not found")
I save into the inserted_photo variable the element which shows me that the photo uploaded successfully. Then if this variable is displayed this means that the photo uploaded properly. Thus, since we have the text and the photo we are ready to click on the 'Post' button.
Try to use static attributes that are not dynamic and there is no danger of future changes. This way you create stability to your code. Because choosing such an xpath like in your example, is risky. If a div or another tag is going to be excluded or included immediately the xpath is not useful.
PS: I uploaded two posts because of testing, so sorry I couldn't try it other way.
I'm trying to login to Office 365 Outlook using Python Selenium, that redirects for 2 factor Auth.
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
email_ID = "YourEmail#Gmail.com"
Password = "Password"
driver = webdriver.Chrome(executable_path="C:\pythonLibs\chromedriver\chromedriver.exe")
driver.set_page_load_timeout(10)
driver.get("https://outlook.office365.com/mail/inbox")
try:
element = WebDriverWait(driver, 10).until
(
# EC.presence_of_element_located((By.ID, "myDynamicElement"))
EC.url_contains("login.microsoftonline.com/common/oauth2/authorize")
)
finally:
print("2nd Login Page Reached.")
print(driver.current_url)
# Login
driver.find_element_by_id("i0116").send_keys(email_ID)
# driver.find_element_by_id("i0118").send_keys(Password) #passwordBrowserPrefill
print("Login Entered")
driver.find_element_by_xpath('//*[#id="idSIButton9"]').submit()
On the the 1st page redirect (2nd Page) after entering email and submitting, it hits me with a page of
Sign in
Sorry, but we’re having trouble with signing you in.
AADSTS90100: login parameter is empty or not valid.
If I comment out the submit, and click Next manually after email has been entered it works as normal.
I have previously tried adding an implicit wait before submitting, but to no avail
I look forward to your suggestions.
Try:
driver.find_element_by_xpath('//*[#id="idSIButton9"]').click()
Instead of:
driver.find_element_by_xpath('//*[#id="idSIButton9"]').submit()
Because it seems that you're submitting the login form before all the required values are entered.
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.