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()
Related
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 am trying to log in to my amboss account using Selenium webdriver with python, but as I dont have much experience with it I dont understand what goes wrong. My credentials (email and password) are correct as I have used them to log into the website before.
Here is my code so far:
# run firefox webdriver from executable path
driver = webdriver.Firefox(firefox_options=options, capabilities=cap, executable_path = path_to_driver)
driver.get("https://www.amboss.com/us/account/login")
signinusername = config['amboss']['email']
signinpassword= config['amboss']['password']
username = driver.find_element_by_id("signin_username")
username.clear()
username.send_keys(signinusername)
pwd = driver.find_element_by_xpath("//*[#id='signin_username']")
pwd.clear()
pwd.send_keys(signinpassword)
loginbutton = driver.find_element_by_xpath("/html/body/div[2]/div[1]/div/div/form/div[4]/input").click()
time.sleep(20)
# execute script to scroll down the page
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;")
#element in log in page
newelement = driver.find_element_by_xpath("//*[#id='left']/p[1]/strong")
print(newelement.get_attribute('innerHTML'))
What I try to do here is log in to the platform and then grab an element which I see in the welcome page by xpath. Despite that selenium is unable to find this element and I get the following error:
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //*[#id='left']/p[1]/strong
Does anyone understand why this happens? Is it because log in was not successful or could something else be wrong? Thanks in advance
UPDATE
The elements you were trying to fetch were in an iframe so you need to switch to that iframe so they be visible. The following works, try it.
username = driver.find_element_by_id("signin_username")
username.clear()
username.send_keys(signinusername)
pwd = driver.find_element_by_id('signin_password')
pwd.clear()
pwd.send_keys(signinpassword)
loginbutton = driver.find_element_by_class_name('amboss-button').click()
time.sleep(2)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;")
time.sleep(2)
frames = driver.find_elements_by_tag_name('iframe')
driver.switch_to.frame(frames[4])
newelement = driver.find_elements_by_tag_name('p')[0].text # This prints the following 'You now have access to AMBOSS—an all-in-one pl....'
print(newelement)
I'm assuming you're trying to login to the system and trying to find something.
First of all your login attempt is not correct. You're entering password in the username field in your code.
Please do login like this:
At first, add this import to your python script:
from selenium.webdriver.common.keys import Keys
Then do login like this:
username = driver.find_element_by_id("signin_username")
username.clear()
username.send_keys(signinusername)
pwd = driver.find_element_by_id("signin_password")
pwd.clear()
pwd.send_keys(signinpassword)
pwd.send_keys(Keys.ENTER)
I'm not logging in by clicking the button. Just hitting an ENTER after logging in.
I guess correcting the login attempt will help you to find your element and print contents inside this.
I've created an account in amboss to answer your question and after logging in the only iframe I found was from google tag manager with no contents in it. So I'm not really sure if your account contains some other web views.
Hope correcting the login attempt helps.
First you wrong this is line :
pwd = driver.find_element_by_xpath("//*[#id='signin_username']")
Please change with :
pwd = driver.find_element_by_id('signin_password')
Second, use conditional if for validation success or failed login :
click login here
time.sleep(20)#here recommendation use WebDriverWait
count = len(driver.find_elements_by_xpath("//*[#id='left']/p[1]/strong"))
if count > 0:
print("login success")
print(driver.find_element_by_xpath("//*[#id='left']/p[1]/strong").text)
else:
print("login failed")
Make sure this is the correct locator : //*[#id='left']/p[1]/strong
I'm trying to do a basic login to a website, but I can't reach 2 objects :
The password field and the button.
The website is: https://www.cms.co.il/default.aspx
I have tried to find element by id or by class - or by XPATH , and I'm finding the "login" text box - but not the "password".
I have checked the name of the attr:
print(driver.find_element_by_css_selector("input#password.top_field.w-input").get_attribute("name"))
driver = webdriver.Chrome()
driver.get(LOGIN_URL)
driver.find_element_by_id("login").clear()
driver.find_element_by_id("login").send_keys(login)
driver.find_element_by_id("password").click()
driver.find_element_by_class_name("top_field w-input").click()
Also, I'm have tried this:
driver.find_element_by_css_selector("input#password.top_field.w-input").send_keys(password)
driver.find_element_by_class_name("top_button.w-button").click()
but it still can't reach the password field
What I'm a doing wrong?
The element have 2 classes, so you have to replace the space with . in your code.
You have to use the below line.
# password
driver.find_element_by_css_selector("input#password.top_field.w-input").send_keys('hello')
# click on button
driver.find_element_by_class_name("top_button.w-button").click()
Screenshot:
solved it ! :)
driver = webdriver.Chrome()
driver.get(LOGIN_URL)
driver.find_element_by_id("login").clear()
driver.find_element_by_id("login").send_keys(login)
driver.find_element_by_id("login").send_keys(Keys.TAB)
time.sleep(3)
actions = ActionChains(driver)
actions.send_keys(password)
actions.perform()
driver.find_element_by_class_name("top_button.w-button").click()
I use webdriver from selenium to download a report from Google AdWords. The problem is that, it seems webdriver doesn't return all links in the page. As it is shown in the attached photo, only links in leftside are returned when I get the pagesource. I need to click on "Run now" link which looks like this in HTML:
Run now
Any comment is appreciated.
Here are the codes I have written so far:
baseurl = "https://accounts.google.com/ServiceLogin?service=adwords&continue=https://adwords.google.com/um/identity?ltmpl%3Djfk%26hl%3Den_US&hl=en_US<mpl=jfk&passive=0&skipvpage=true"
username = "myUsername"
password = "myPassword"
xpaths = { 'usernameTxtBox' : "//input[#name='Email']",
'passwordTxtBox' : "//input[#name='Passwd']",
'submitButton' : "//input[#name='signIn']"
}
mydriver = webdriver.Firefox()
mydriver.get(baseurl)
mydriver.maximize_window()
#Clear Username TextBox if already allowed "Remember Me"
mydriver.find_element_by_xpath(xpaths['usernameTxtBox']).clear()
#Write Username in Username TextBox
mydriver.find_element_by_xpath(xpaths['usernameTxtBox']).send_keys(username)
#Clear Password TextBox if already allowed "Remember Me"
mydriver.find_element_by_xpath(xpaths['passwordTxtBox']).clear()
#Write Password in password TextBox
mydriver.find_element_by_xpath(xpaths['passwordTxtBox']).send_keys(password)
#Click Login button
mydriver.find_element_by_xpath(xpaths['submitButton']).click()
#Here when I print the source, I do not see any element from the table in the middle of the page
print mydriver.page_source
Finally I could figure out how it can be done, by adding the following code before I open the url:
mydriver.implicitly_wait(60)
and then:
driver.find_element_by_link_text("Run now").click()
The implicit wait waits to get the elements loaded from DOM. So it is the only solution to do that.
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")