I am using Selenium Webdriver for the first time and am running a very simple script, but it is not working. I would like to open Firefox, go to LinkedIn, and enter my email address in the email login field. Using the code below, I'm able to get the first two operations to work, but the script is not properly identifying the email field, and so my email address is never being typed in anywhere.
browser = webdriver.Firefox() #Get local session of firefox
browser.get("http://www.linkedin.com") #Load page
elem = browser.find_element_by_name("session_key-login") #Find the login box
elem.send_keys("email#gmail.com" + Keys.RETURN) #Enter email into login box
How do I correctly identify the email login box and pass it to "elem"?
Try giving
element.send_keys("email#gmail.com");
instead of
elem.send_keys("email#gmail.com" + Keys.RETURN)
How about you just do
elem.send_keys("email#gmail.com")
UPDATE
The identifier used by you was incorrect, you can use either one of the below.
elem = browser.find_element_by_id("session_key-login")
elem = browser.find_element_by_name("session_key")
Related
I am a novice coder. Trying my hand at creating an instagram bot and I'm following the instructions found here: (https://realpython.com/instagram-bot-python-instapy/)
I can fill in the elements for user name and password, but I'm having trouble clicking the login link. Using the -
#submit = browser.find_element_by_tag_name('form')
#submit.submit()
portion of my code works to log in, but I would like to be able to use find element by xpath and for this so I can apply it in different situations. If someone could please let me know where I can look in the HTML on the instagram page to find what I'm looking for, or direct me towards any helpful reading, I'd really appreciate it! I've tried a few ways to do this.
Here is my code. I also added photos of my code and error message.
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
#sets browser to firefox - need to make sure that geckodriver and firefox are in the path
browser = webdriver.Firefox()
#opens firefox to instagram#
browser.get('https://www.instagram.com/accounts/login/?source=auth_switcher')
#tells selenium to wait 5 seconds before trying again if it can't find something
browser.implicitly_wait(10)
print ('i waited')
user = 'username'
password = 'password'
##### USERNAME #####
username = browser.find_element_by_xpath("//input[#name='username']")
username.send_keys(user)
#username = browser.find_element_by_name('username')
#username.send_keys(user)
##### PASSWORD #####
password = browser.find_element_by_xpath("//input[#name='password']")
password.send_keys(password)
#password = browser.find_element_by_name('password')
#password.send_keys(password)
browser.implicitly_wait(10)
##### LOG IN #####
#instead of searching for the Button (Log In) you can simply press enter when you already selected the password or the username input element.
#submit = browser.find_element_by_tag_name('form')
#submit.submit()
#login_link = browser.find_element_by_xpath("//article/div/div/p/a[text()='Log in']")
login_link = browser.find_element_by_xpath("//a[text()='Log in']")
login_link.click()
#login_form = browser.find_element_by_xpath("//form[#id='loginForm']")
#form.submit()
print ('login incomplete :)')
sleep(5)
browser.quit() #quits geckodriver and mozilla
print ('closed')
Picture of error
code part 1
code part 2
Your xpath is bit wrong. you are using
//a[text()='Log in']
but Log in is inside div tag, not a tag.
Please use this xpath
//div[text()='Log In']/..
In your code
login_link = browser.find_element_by_xpath("//div[text()='Log In']/..")
login_link.click()
The HTML is
<div class=" qF0y9 Igw0E IwRSH eGOV_ _4EzTm ">Log In</div>
and we are using text() to target text Log in and then we are looking for button using /.. which is a parent node.
Replace this link
login_link = browser.find_element_by_xpath("//a[text()='Log in']")
with
login_link = browser.find_element_by_xpath("//div[contains(text(),'Log In')]")
And you should be good. Since the Login In button is wrapped inside a div element and not a element.
As already explained Log In is in a div tag under a button tag with type submit. You can use this xpath too.
browser.find_element_by_xpath("//button[#type='submit']").click()
I'am new to mobile automation. I thought I got the gist, but I ran into a problem.
An error occurs when using the command send_keys()
selenium.common.exceptions.InvalidElementStateException: Message: Cannot set the element to '321785214'. Did you interact with the correct element?
I have a code
driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_cap)
# Skip fingerprint
driver.find_element_by_xpath("//android.widget.Button[#bounds='[413,1802][668,1877]']").click()
# Click Continue
driver.find_element_by_xpath("//android.widget.Button[#content-desc='Continue']").click()
# Click to input phone number
input_phone_number = driver.find_element_by_xpath("//android.widget.EditText").click()
input_phone_number_edit = driver.find_element_by_class_name("android.widget.EditText")
# Enter phone number
input_phone_number_edit.send_keys("321785214")
And I have code from other mobile app and this code is working
driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_cap)
# click to Set up later button
set_up_later_btn = driver.find_element_by_xpath('//android.widget.Button[#content-desc="Set up later"]').click()
input_phone_number = driver.find_element_by_xpath('//android.view.View[#content-desc="Phone number"]').click()
input_phone_number_edit = driver.find_element_by_class_name('android.widget.EditText')
driver.implicitly_wait(50)
input_phone_number_edit.send_keys('178546128')
driver.find_element_by_accessibility_id("Continue").click()
I don't know why first code isn't working. I need help. Thanks
Instead of using the default send_keys method, you can use the send_keys method provided by the ActionChains class
from selenium.webdriver import ActionChains
actions = ActionChains(self.driver)
actions.send_keys("321785214")
actions.perform()
I've gone through a number of similar topics here but they all seem to vary in how the pop-up window is designed. I've tried a few different ways and here is the most recent. So before I enter the login info, I need to click that client login button to access the login form but I can't even get it to open, let alone entering login information.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('C:\Login Automation\chromedriver.exe')
driver.get("https://www.datamyne.com/ ")
clientlogin = driver.find_element_by_xpath("//div[#id='holder']").click()
username = driver.find_element_by_xpath("//*[#id='user']").send_keys('myusername')
password = driver.find_element_by_xpath("//*[#id='pass']").send_keys('mypassword')
the error I'm getting here is "NoSuchWindowException: Message: no such window: target window already closed from unknown error: web view not found"
the element of the first button is this:
<a style="position: relative" href="javascript:showHide('dialog-login');" class="green-btn user-login top-right-5">Client Login</a>
and then the actual login button is another javascript line:
Login
Any tips as to how to approach this would be really appreciated!
I have inspect mention website login Form based on JavaScript. You can easily execute script through selenium. I have create a basic code snippet for you.
from selenium import webdriver
driver = webdriver.Chrome('chromedriver.exe')
driver.get("https://www.datamyne.com/")
##Javascript script execute using selenium
clientlogin = driver.execute_script("javascript:showHide('dialog-login');")
driver.implicitly_wait(5)
username = driver.find_element_by_xpath('//*[#id="User"]').send_keys('myusername')
password = driver.find_element_by_xpath('//*[#id="Pass"]').send_keys('mypassword')
save = driver.find_element_by_xpath('//*[#id="formLoginDM"]/div[1]/a').click()
clientlogin = driver.find_element_by_xpath("//div[#id='holder']").click()
The xpath is off. The holder isn't what you need to click.
I suspect you want:
clientlogin = driver.find_element_by_xpath("//a[text()='Client Login']").click()
Can you try this xpath once for the 'Client Login' pop-up modal
clientlogin = driver.find_element_by_xpath("//a[#class='green-btn user-login top-right-5']']").click()
I'm trying to automate a duolingo login with Selenium with the code posted below.
While everything seems to work as expected at first, I always get an "Wrong password" message on the website after the login button is clicked.
I have checked the password time and time again and even changed it to one without special characters, but still the login fails.
I have seen in other examples that there is sometimes an additional password input field, however I cannot find one while inspecting the html.
What could I be missing ?
(Side note: I'm also open to a completely different solution without a webdriver since I really only want to get to the duolingo.com/learn page to scrape some data, but as of yet I haven't found an alternative way to login)
The code used:
from selenium import webdriver
from time import sleep
url = "https://www.duolingo.com/"
def login():
driver = webdriver.Chrome()
driver.get(url)
sleep(2)
hve_acnt_btn = driver.find_element_by_xpath("/html/body/div/div/div/span[1]/div/div[1]/div[2]/div/div[2]/a")
hve_acnt_btn.click()
sleep(2)
email_input = driver.find_element_by_xpath("/html/body/div[1]/div[3]/div[2]/form/div[1]/div/label[1]/div/input")
email_input.send_keys("email#email.com")
sleep(2)
pwd_input = driver.find_element_by_css_selector("input[type=password]")
pwd_input.clear()
pwd_input.send_keys("password")
sleep(2)
login_btn = driver.find_element_by_xpath("/html/body/div[1]/div[3]/div[2]/form/div[1]/button")
login_btn.click()
sleep(5)
login()
I couldn't post the website's html because of the character limit, so here is the link to the duolingo page: Duolingo
Switch to Firefox or a browser which does not tell the page that you are visiting it automated. See my earlier answer for a very similar issue here: https://stackoverflow.com/a/57778034/8375783
Long story short: When you start Chrome it will run with navigator.webdriver=true. You can check it in console. Pages can detect that flag and block login or other actions, hence the invalid login. This is a read-only flag set by the browser during startup.
With Chrome I couldn't log in to Duolingo either. After I switched the driver to Firefox, the very same code just worked.
Also if I may recommend, try to use Xpath with attributes.
Instead of this:
hve_acnt_btn = driver.find_element_by_xpath("/html/body/div/div/div/span[1]/div/div[1]/div[2]/div/div[2]/a")
You can use:
hve_acnt_btn = driver.find_element_by_xpath('//*[#data-test="have-account"]')
Same goes for:
email_input = driver.find_element_by_xpath("/html/body/div[1]/div[3]/div[2]/form/div[1]/div/label[1]/div/input")
vs:
email_input = driver.find_element_by_xpath('//input[#data-test="email-input"]')
I have just started using selenium with Python for the first time, after following a quick tutorial I am now trying to make a program with it that will login to Gmail and then send an email to a chosen email address.
I've gotten the login part done but had some problems with the composing a new email part (only works some of the time) and I get stuck everytime when it comes to writing the message body.
My code is below, I have tried reading the docs but Im having trouble getting the following to work in Gmail and the when I inspect the elements in gmail it seems a lot more complex than the basic html structures in the examples here:
http://selenium-python.readthedocs.org/locating-elements.html#locating-elements-by-tag-name
"""
Write a program that takes an email address and string of text on the command line and then, using Selenium,
logs into your email account and sends an email of the string to the provided address.
"""
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser = webdriver.Chrome()
browser.get('http://www.gmail.com')
emailElem = browser.find_element_by_id('Email')
emailElem.send_keys('My_email#gmail.com')
emailElem.submit()
time.sleep(2)
passwordElem = browser.find_element_by_id('Passwd')
passwordElem.send_keys('My_password_here')
passwordElem.submit()
time.sleep(2)
composeElem = browser.find_element_by_class_name('z0') #this only works half of the time
composeElem.click()
time.sleep(7)
toElem = browser.find_element_by_name("to")
toElem.send_keys('my_other_email#gmail.com')
time.sleep(2)
subjElem = browser.find_element_by_name("subjectbox")
subjElem.send_keys('Test with selenium')
time.sleep(2)
bodyElem = browser.find_element_by_???('???') #this is where I get stuck and not sure what to do here
bodyElem.send_keys('A test email with selenium')
time.sleep(2)
sendElem = browser.find_element_by_link_text('send') #not sure if this is correct too
sendElem.submit()
I think the easiest way to select elements on a loaded page is to find them by css selector. You can find them in the browser inspector, and then copy their unique css selector (in firefox press inspect element -> copy unique selector). In this case this should work:
browser.find_element_by_css_selector('#\:nw')
Please Try :
time.sleep(10)
bodyElem = browser.find_element_by_xpath("//*[#id=":ov"]")
OR
bodyElem = browser.find_element_by_xpath("//*[#id=":ou"]")
I assume that it need little more time to find element so I have increased sleep time and also given xpath should work.
As id is changing each time, we can check for aria-label :
browser.find_element_by_css_selector("div[aria-label='Message Body']")
For send, use this :
sendElem = browser.find_element_by_xpath("//div[text()='Send']")
sendElem.click()
You can also hack it using the following once you've found the message box (I'm a noob and couldn't get Send to work)
sendElem.send_keys(Keys.TAB)
sendElem.send_keys(Keys.CONTROL + Keys.RETURN)