Clicking a LinkedIn button using selenium (python)

对着背影说爱祢 提交于 2019-12-25 01:28:02

问题


noob here having a heck of time trying to click on a webelement within LinkedIn with no success. To begin here is the source code I'm dealing with:

this is my login:

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

this is the block that I'm trying to find.

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

I can login and and navigate to the page just fine (after you set a sleep for linkedIn to load past a firewall), but I have done everything to try to click on the button with no success.

I've tried:

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

and

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

nothing.... any help would be much appreciated. I haven't been able to click on LinkedIn buttons or elements since it changed it's platform around this time in 2016.

These are the 3 errors I'm getting:

  1. An element command could not be completed because the element is not visible on the page.
  2. An element could not be located on the page using the given search parameters.
  3. Unknown server-side error.

Thanks, Chris


回答1:


This was the line that finally got the job done... just brings up another (different) issue, but progress none the less.

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



回答2:


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

try using css selector (via classes)




回答3:


Asper the HTML you have provided to click() on the button with text as Connect you can use the following line of code :

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



回答4:


trying to do the same thing here and found out the LinkedIn hide the page source using javascript which is not readable by the WebDriver as there is no HTML to work with. I am using this code to get the inner HTML but having issues clicking the connect button. You will notice that the actual HTML is inside data variable, how can I get the click to work in this situation. Hany help would be highly appreciable. this code is a small part of a large project which does a lot of things on 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")


来源:https://stackoverflow.com/questions/48120556/clicking-a-linkedin-button-using-selenium-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!