如何使用python selenium点击链接?

时间:2016-05-28 05:39:53

标签: python selenium web-crawler linkedin

我正在尝试使用python selenium登录我的linkedin。我可以打开我的主页,但之后我想在我的主页上打开以下链接

<a href="/profile/edit?trk=nav_responsive_sub_nav_edit_profile">
Edit Profile
</a>

我使用以下代码允许我打开我的主页 -

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()

但是我收到以下错误消息 -

  

selenium.common.exceptions.NoSuchElementException:消息:无法执行   定位元素:{&#34;方法&#34;:&#34;链接文字&#34;,&#34;选择器&#34;:&#34;编辑个人资料&#34;}

3 个答案:

答案 0 :(得分:1)

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()

答案 1 :(得分:1)

通过明确等待页面上存在的元素,可能会解决您的问题。请参阅以下文档的5.1显式等待

http://selenium-python.readthedocs.io/waits.html

这样的事情:

wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID, 'someid')))

注意:输入用户名,密码并点击提交

后,应使用显式等待

答案 2 :(得分:0)

似乎&#34;编辑个人资料&#34;只是&#34; Profile&#34;的选项。下拉菜单,因此初始不可见。你应该点击&#34; Profile&#34;首先打开下拉菜单。尝试:

 if (!IsPostBack)
        {
            foreach (Organisation org in Database.data)
            {
                if (org.name.Equals(Session["Org"]))
                {
                    orgLabel.Text = org.name;
                      lastcontractorBox.Text = org.id.ToString();
                    buildingAddress.Text = org.address;
                }
            }
        }

<强>更新

使用driver.find_element_by_link_text("Profile").click() driver.find_element_by_link_text("Edit Profile").click()

XPath