使用 selenium 单击 LinkedIn 按钮 (python)

Clicking a LinkedIn button using selenium (python)

新手花了很多时间尝试点击 LinkedIn 中的网络元素,但没有成功。从这里开始是我正在处理的源代码:

这是我的登录信息:

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

time.sleep(15)
driver.get("https://www.linkedin.com/search/results/people/facetNetwork=%5B%22S%22%5D&keywords=software%20engineers&origin=FACETED_SEARCH")

这是我要寻找的区块。

<button aria-label="Connect with LJ Wilson" class="search-result__actions--primary button-secondary-medium m5" data-ember-action="" data-ember-action-5373="5373" data-is-animating-click="true">
  Connect
</button>

我可以正常登录并导航到该页面(在您为 linkedIn 设置睡眠以通过防火墙加载之后),但我已尽一切努力尝试点击按钮但没有成功。

我试过:

driver.find_element_by_xpath("//button[@class='search-result__actions--primary button-secondary-medium m5']"[1]).click()

driver.find_element_by_xpath("//button[contains(text()="Connect])).click()

没有...任何帮助将不胜感激。我一直无法点击 LinkedIn 按钮或元素,因为它在 2016 年的这个时候改变了它的平台。

这些是我遇到的 3 个错误:

  1. 元素命令无法完成,因为该元素在页面上不可见。
  2. 无法使用给定的搜索参数在页面上找到元素。
  3. 未知的服务器端错误。

谢谢, 克里斯

driver.find_element_by_css_selector(".search-result__actions--primary.button-secondary-medium.m5")

尝试使用 css 选择器(通过 类)

根据您在按钮上提供给 click()HTML,文本为 Connect 您可以使用以下代码行:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='search-result__actions--primary button-secondary-medium m5' and contains(.,'Connect')]"))).click()

这是最终完成工作的路线...只是提出了另一个(不同的)问题,但进展none较少。

driver.execute_script("document.getElementsByClassName('search-result__actions--primary button-secondary-medium m5')[1].click();")

尝试在这里做同样的事情,发现 LinkedIn 使用 javascript 隐藏页面源,WebDriver 无法读取它,因为没有 HTML 可以使用。我正在使用此代码获取内部 HTML 但在单击连接按钮时遇到问题。您会注意到实际的 HTML 在数据变量中,在这种情况下我怎样才能让点击工作。非常感谢 Hany 的帮助。这段代码是一个在 LinkedIn 上做很多事情的大项目的一小部分。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import WebDriverException
import time
from time import sleep
from bs4 import BeautifulSoup
from tqdm import tqdm
import csv
from urllib.parse import urljoin
import re
import sys
import colorama
import random

email = input("Your Login Email Please: ")
print(email)

password = input("Your Password Please: ")
print(password)

time_delay = int(input("Please Enter Delay In Secs For Randomization: "))
print(time_delay)


option = webdriver.ChromeOptions()
option.add_argument("--normal")
option.add_argument("--start-maximized")
option.add_argument("--disable-extensions")
option.add_argument("--auto-open-devtools-for-tabs")
option.add_argument("--disable-infobars")
option.add_argument("--disable-extensions")

driver = webdriver.Chrome(executable_path=r"C:\Users\Rohit.METRO-ROHIT\Desktop\Selenium Development\chromedriver.exe", chrome_options=option)
Development\chromedriver.exe", chrome_options=option)

driver.get('https://www.linkedin.com')

email_box = driver.find_element_by_id('login-email')
email_box.send_keys(email)
time.sleep(random.random() * time_delay)
pass_box = driver.find_element_by_id('login-password')
pass_box.send_keys(password)
time.sleep(random.random() * time_delay)
submit_button = driver.find_element_by_id('login-submit')
submit_button.click()

with open('lets_connect.csv') as example_file:
example_reader = csv.reader(example_file)
for row in example_reader:
time.sleep(random.random() * time_delay*10)
driver.get(row[0])
time.sleep(random.random() * time_delay)
driver.refresh()
print("refreshing the current page")
time.sleep(random.random() * time_delay*2)
demo_div = driver.find_element_by_tag_name('body')
print (demo_div.get_attribute('innerHTML').encode('UTF-8').decode('UTF-8'))
data = (driver.execute_script("return arguments[0].innerHTML", demo_div))
print(data.encode('UTF-8').decode('UTF-8'))
soup = BeautifulSoup(data, "lxml")
try:
connect_button = driver.find_element_by_xpath('//*[@id="ember15196"]/div[2]/div[2]/button[1]')
print("test")
print(connect_button)
print("test")
try:
connect_button.click()
except:
print("cant click")
profile-actions--connect button-primary-large mh1').click()
except WebDriverException:
print ("Connect Button Not Found")