簡體   English   中英

使用Selenium(Python)單擊LinkedIn按鈕

[英]Clicking a LinkedIn button using selenium (python)

Noob在這里花費了很長時間嘗試單擊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>

我可以登錄並導航到該頁面(在您設置了linkIn的休眠狀態以加載通過防火牆之后),但是我已經做了一切嘗試單擊該按鈕都沒有成功。

我試過了:

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.execute_script("document.getElementsByClassName('search-result__actions--primary button-secondary-medium m5')[1].click();")
driver.find_element_by_css_selector(".search-result__actions--primary.button-secondary-medium.m5")

嘗試使用CSS選擇器(通過類)

Asper提供的HTML可以為按鈕上的click()提供click()帶有文本為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()

嘗試在此處做同樣的事情,發現LinkedIn使用Javascript隱藏了頁面源,而Javascript無法被WebDriver讀取,因為沒有可使用的HTML。 我正在使用此代碼來獲取內部HTML,但是在單擊連接按鈕時遇到了問題。 您會注意到實際的HTML在數據變量內部,在這種情況下如何使單擊起作用。 漢尼的幫助將非常可觀。 這段代碼只是一個大型項目的一小部分,該項目在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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM