how to click on the page in facebook using python selenium - python

when i compile my code it log in but the line that should click on the page does not work.
thats my code :
from selenium import webdriver
from Config import keys
import time
def order(k):
driver = webdriver.Chrome('driver\chromedriver.exe')
driver.get(k['product_url'])
username ="gmail"
pw ="password"
#Enter The Pw & email fb
driver.find_element_by_xpath('//*[#id="email"]').send_keys(username)
driver.find_element_by_xpath('//*[#id="pass"]').send_keys(pw)
#Click on cnx button
driver.find_element_by_xpath('//*[#id="u_0_b"]').click()
#go to the page
# here is the problem
driver.find_element_by_xpath('//*[#id="mount_0_0"]/div/div/div[1]/div[3]/div/div[1]/div/div/ul/li[2]/span/div/a').click()
driver.find_element_by_xpath('//*[#id="mount_0_0"]/div/div/div[2]/div/div/div/div/div/div[2]/div/div/div/div[3]/div/div/div[1]/div/a/span').click()
driver.page_source()
if __name__=='__main__':
order(keys)

Could be due to synchronization issue try to induce WebDriverWait :
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//*[#id='mount_0_0']/div/div/div[1]/div[3]/div/div[1]/div/div/ul/li[2]/span/div/a"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//*[#id='mount_0_0'']/div/div/div[2]/div/div/div/div/div/div[2]/div/div/div/div[3]/div/div/div[1]/div/a/span"))).click()
Note : please add below imports to your solution
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

After checking on the Facebook website, '//*[#id="mount_0_0"]' is the xpath of the main container. For the button you are trying to click, the xpath I have found is :
//*[#id="mount_0_0"]/div/div/div[1]/div[3]/div/div[1]/div/div/ul/li[2]/span/div/a
Maybe you could try with this one ?

Related

Selecting a specific element with Selenium - Python

I'm trying to click this button shown after choosing an option from a drop-down menu.
This is the button I'm trying to click.
The link to the website: https://excise.wb.gov.in/CHMS/Public/Page/CHMS_Public_Hospital_Bed_Availability.aspx
The html:
I've tried using XPATH, and through visible text; nothing seems to work.
My code as of now:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import Select
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://excise.wb.gov.in/CHMS/Public/Page/CHMS_Public_Hospital_Bed_Availability.aspx")
drop_dist = \
driver.find_element(By.XPATH, "/html/body/form/div[3]/main/div[2]/div/div[2]/div/div[1]/div[2]/div/select")
select_dist = Select(drop_dist)
select_dist.select_by_value("005")
l = driver.find_element(By.XPATH, "/html/body/form/div[3]/main/div[2]/div/div[2]/div/div[4]/div/div/table/tbody/tr[1]/td/div/div[2]/div[2]/div[1]/a").click()
time.sleep(30)
Any help is much appreciated!
The locator seems fragile, use following locator and webdriverwait() to handle sync issue.
driver.get("https://excise.wb.gov.in/CHMS/Public/Page/CHMS_Public_Hospital_Bed_Availability.aspx")
wait=WebDriverWait(driver, 20)
select_dist =Select(wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#ctl00_ContentPlaceHolder1_ddl_District"))))
select_dist.select_by_value("005")
wait.until(EC.element_to_be_clickable((By.XPATH, "(//a[#class='btn btn-link' and contains(., 'View Detail Break up')])[1]"))).click() //this will click the first one only.
You have to add following imports.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Try this:
button = driver.find_elements(By.CSS, 'a[role="button"][aria-expanded="false"]')[0]
button.click()
You can change the index in order to click on other elements

No such element exception Selenium

I'm trying to login to a webpage using Selenium but when I enter the relevant email address and try to click continue I get the no such element exception.
driver = webdriver.Safari()
driver.get('https://manage.statuspage.io/login')
email = driver.find_element_by_id('email')
email.clear()
email.send_keys('XXX')
driver.find_element_by_id("login-btn").click()
driver.find_element_by_id("login-submit").click()
The error occurs on the last line.
As you can see I'm searching for this continue button via id 'login-submit' but it says that it doesn't exist.
Any help would be great!
After clicking
driver.find_element_by_id("login-btn").click()
It takes you to another page where the login-submit element is presented.
But you trying to click this element immediately after clicking on element on previous page while the new page is still not loaded.
You should add a wait to wait for that element on the second page to appear before accessing it.
This should work:
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
driver = webdriver.Safari()
wait = WebDriverWait(driver, 20)
driver.get('https://manage.statuspage.io/login')
email = wait.until(EC.visibility_of_element_located((By.ID, "email")))
email.clear()
email.send_keys('XXX')
wait.until(EC.visibility_of_element_located((By.ID, "login-btn"))).click()
wait.until(EC.visibility_of_element_located((By.ID, "login-submit"))).click()
You need to wait for the element/locator to appear on the webpage.
from selenium.webdriver.support import expected_conditions as EC
driver.find_element_by_id("login-btn").click()
login_button = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.ID, 'login-submit')))
login_button.click()
You can use explicitWait like below to click on the desired button
driver.find_element_by_id("email").send_keys("email")
OR
email = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.ID, "email")))
email.send_keys('email')
btnLogin = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"login-btn")))
btnLogin.click()
import
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
What happened sometime when you send request from a script it returns you page with different ids or class names. After driver.get('https://manage.statuspage.io/login') line save the webpage as .html file and see the id or class name there.

Perform a click on a web app with selenium python (repost)

I am trying to scrape or crawl this web app (https://www.ea.com/en-gb/fifa/ultimate-team/web-app/) I not sure if its because its a web app or some anti scraper measures but nothing is happening when I attempt to click the login button.
the button is clicked but doesn't show anything.
Code
driver = webdriver.Chrome('/Users/Downloads/chromedriver')
driver.get("https://www.ea.com/en-gb/fifa/ultimate-team/web-app/")
driver.implicitly_wait(20)
driver.find_element_by_xpath('//*[#id="Login"]/div/div/button[1]').click()
The xpath needs to be updated. Note that the xpath I used is not the only way to di this button[contains(text(), "Login")] would also work in this scenario. And I would use By, WebDriverWait, and expected_conditions instead of driver.implicitly_wait(20) see below:
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
driver = webdriver.Chrome('/Users/Downloads/chromedriver')
driver.get("https://www.ea.com/en-gb/fifa/ultimate-team/web-app/")
click_this = WebDriverWait(driver, 10).until(EC.element_to_be_clickable(
(By.XPATH, '//*[#id="Login"]//*//button[contains(text(), "Login")]')))
click_this.click()

Login Button can not be found with selenium

https://www.sevenonemedia.de/tv/programm/programmwochen
Here I want to login:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
options = Options()
chrome_path = "T:/Markus/WebScrapingExample/Chromedriver/chromedriver.exe"
driver = webdriver.Chrome(executable_path=chrome_path,chrome_options=options)
driver.set_window_size(1280, 720)
time.sleep(5)
driver.get("https://www.sevenonemedia.de/tv/programm/programmwochen")
driver.find_element_by_id("_58_login").send_keys("name")
driver.find_element_by_id("_58_password").send_keys("pw")
driver.find_element_by_xpath('//*[#id="sign-in-button"]').click()
ElementNotInteractableException: element not interactable
(Session info: chrome=78.0.3904.97)
This is my error
Id is there. Why does this happen?
This page contains duplicate of element with ID sign-in-button. If your selector points to more than one element, driver always takes the first from the top of the DOM one which is not interactable in this case. You must refer to the second element with this id. Try this selector for "Sign in" button:
//*[#id="aheadcustom_p_p_id_58"]//button
hi first things you should not give your password to all of the stackoverflow community :)
you can't click on the button because there is a popup at the bottom of the page and you have to click on it first for selenium it's hidding your button
last is that the totality of your code ?
if yes you forgot
driver = webdriver.Firefox() #or any other webdriver
you have not create driver without this line
EDIT !!
it wasn't working with only the modification above but with this one its good
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
time.sleep(5)
driver = webdriver.Firefox()
driver.get("https://www.sevenonemedia.de/tv/programm/programmwochen")
driver.find_element_by_id("_58_login").send_keys("login")
driver.find_element_by_id("_58_password").send_keys("pssd")
driver.find_element_by_xpath("/html/body/div[1]/div/div/div[2]/a").click()
driver.find_element_by_css_selector("#_58_fm > fieldset:nth-child(1) > div:nth-child(6) > button:nth-child(1)").click()
this work :)
sometime css selector a safer and work better
To click on the Login button you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solutions:
Using XPATH:
driver.get("https://www.sevenonemedia.de/tv/programm/programmwochen")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#title='Anmelden' and not(contains(#name,'INSTANCE'))]"))).send_keys("a.mai#mediaplus.com")
driver.find_element_by_xpath("//input[#title='Passwort' and not(contains(#name,'INSTANCE'))]").send_keys("Edidaten17")
driver.find_element_by_xpath("//input[#title='Passwort' and not(contains(#name,'INSTANCE'))]//following::button[1]").click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:
The problem is that your button locator isn't unique on the page. It finds two buttons, the first of which is not visible which causes the ElementNotInteractableException.
The simple fix is to use the CSS selector below
#main-content #sign-in-button
That will find only the button you want. So your last few lines of code would be
driver.find_element_by_id("_58_login").send_keys("name")
driver.find_element_by_id("_58_password").send_keys("pw")
driver.find_element_by_css_selector('#main-content #sign-in-button').click()
sleep(1)
login_box = driver.find_element_by_name('login')
login_box.click()
this is for facebook login button, just inspect the website and see the id/name/type to make your code automate to work properly

how to click on the link using python selenium?

I am trying to log in into my linkedin using python selenium. I am able to open my homepage but after that I want to open the following link present on my homepage
<a href="/profile/edit?trk=nav_responsive_sub_nav_edit_profile">
Edit Profile
</a>
I used the following code which allows me to open my homepage-
import getpass
from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
url = "https://www.linkedin.com/uas/login"
driver = webdriver.Firefox()
driver.get(url)
username = 'email-id'
password = 'password'
user = driver.find_element_by_name("session_key")
for j in username:
user.send_keys(j)
pasw = driver.find_element_by_name("session_password")
for j in password:
pasw.send_keys(j)
driver.find_element_by_css_selector("div.form-buttons>input").click()
driver.find_element_by_link_text("Edit Profile").click()
but i get the following error message-
selenium.common.exceptions.NoSuchElementException: Message: Unable to
locate element: {"method":"link text","selector":"Edit Profile"}
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.common.exceptions import TimeoutException
import urllib, os, urllib.request
import time
driver = webdriver.Safari()
usrName = 'your_email'
pssWrd = "your_password"
driver.maximize_window()
driver.get("https://www.linkedin.com/uas/login?")
driver.find_element_by_name('session_key').send_keys(usrName)
driver.find_element_by_class_name('password').send_keys(pssWrd)
driver.find_element_by_name('signin').click()
It sounds likely that your problem can be solved by waiting explicitly for the elements to be present on the page. See section 5.1 Explicit Waits of the following documentation
http://selenium-python.readthedocs.io/waits.html
Something like this:
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID, 'someid')))
Note: the explicit wait should be used after you've entered your username, password and clicked submit
Seems that "Edit Profile" is just an option for "Profile" drop-down menu, so it's not visible initialy. You should click on "Profile" first to open drop-down menu. Try:
driver.find_element_by_link_text("Profile").click()
driver.find_element_by_link_text("Edit Profile").click()
UPDATE
Using XPath:
driver.find_element_by_xpath('//a[#class="nav-link"][contains(text(),"Profile")]').click()
driver.find_element_by_xpath('//a[#href="/profile/edit?trk=nav_responsive_sub_nav_edit_profile"]').click()

Categories

Resources