how to click on the link using python selenium?
16,885
Solution 1
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
Solution 2
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()
Author by
cgkentrus
Updated on June 13, 2022Comments
-
cgkentrus almost 2 years
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"}
-
Saurabh Gaur almost 8 yearsYou can use Xpath here to find link and click..because may be link text has extra spaces or \t,\n...which is not found by link text...
-
cgkentrus almost 8 yearsdriver.find_element_by_xpath("html/body/div[2]/div[2]/div/div[1]/ul/li[2]/ul/li[1]/a").click() is this the right format for using Xpath to find link and click on it?
-
-
cgkentrus almost 8 yearsI tried your suggestion but got the following error:selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"link text","selector":"Profile"}
-
Andersson almost 8 yearsAre you sure that you pass authorization and you have access to profile editing option?
-
cgkentrus almost 8 yearsYes, I am able to login successfully onto by homepage using the above mentioned code, it even opens up in my firefox which displayes the whole automated process of logging in,but after that it doesn't do anything.
-
cgkentrus almost 8 yearsThanks for the update. I tried the Xpath method still cannot click on that link-selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":"//a[@class=\"nav-link\"][contains(text(),\"Profile\")]"}
-
Andersson almost 8 yearshm... it's weird as both options works for me... I don't know where are you from, so just to be sure: your LinkedIn menu appears in English?
-
cgkentrus almost 8 yearsMy LinkedIn menu appears in English only. For logging in linkedin have you tried the same process mentioned above or something else?
-
cgkentrus almost 8 yearsIt worked, thanks I just introduced time.sleep() so after the page is fully loaded then only it begins to click on the specified Xpath.
-
JeffC almost 8 yearsDon't use a .sleep(). Use a WebDriverWait instead and wait for the element to be clickable... then click it. .sleep() is not dynamic enough... it may not be long enough some times and too long others. A proper wait will execute as soon as the element is available but time out if it takes too long.
-
Paula Livingstone over 5 yearsThis doesn't answer the question
-
Chris Brocious over 5 yearswhat am I missing?
-
Paula Livingstone over 5 yearsI think the crux of the OPs thrust is to get to the "Edit Profile" page.
-
Chris Brocious over 5 yearsGotcha. The web elements are completely different now. Should I update with a redirect to the edit page now?