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()
Share:
16,885
cgkentrus
Author by

cgkentrus

Updated on June 13, 2022

Comments

  • cgkentrus
    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
      Saurabh Gaur almost 8 years
      You 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
      cgkentrus almost 8 years
      driver.find_element_by_xpath("html/body/div[2]/div[2]/div/di‌​v[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
    cgkentrus almost 8 years
    I tried your suggestion but got the following error:selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"link text","selector":"Profile"}
  • Andersson
    Andersson almost 8 years
    Are you sure that you pass authorization and you have access to profile editing option?
  • cgkentrus
    cgkentrus almost 8 years
    Yes, 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
    cgkentrus almost 8 years
    Thanks 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\"][conta‌​ins(text(),\"Profile‌​\")]"}
  • Andersson
    Andersson almost 8 years
    hm... 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
    cgkentrus almost 8 years
    My LinkedIn menu appears in English only. For logging in linkedin have you tried the same process mentioned above or something else?
  • cgkentrus
    cgkentrus almost 8 years
    It 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
    JeffC almost 8 years
    Don'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
    Paula Livingstone over 5 years
    This doesn't answer the question
  • Chris Brocious
    Chris Brocious over 5 years
    what am I missing?
  • Paula Livingstone
    Paula Livingstone over 5 years
    I think the crux of the OPs thrust is to get to the "Edit Profile" page.
  • Chris Brocious
    Chris Brocious over 5 years
    Gotcha. The web elements are completely different now. Should I update with a redirect to the edit page now?