1

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

3
  • 1
    What do you mean by "no success" and "nothing"? Do you literally mean that you get no errors or warnings, but the browser doesn't load the new page? Commented Jan 5, 2018 at 20:27
  • I'm sorry yes I get errors. One of which says element not found and another said unexpected server-side error. Commented Jan 5, 2018 at 20:51
  • Please edit your question to show the exact errors. Commented Jan 5, 2018 at 20:55

4 Answers 4

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();")
Sign up to request clarification or add additional context in comments.

Comments

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

try using css selector (via classes)

2 Comments

Get the same "An element command could not be completed because the element is not visible on the page." -it is visible... or I'm hallucinating
can you share the link as well?
0

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

8 Comments

Tried it. Said that the element wasn't visible. I've maximized the page (I've seen this before)... still says not visible. I put an [0] index in the line, and the error goes back to "Message: An unknown server-side error occurred while processing the command."
the WebDriverWait doesn't seem to work on linkedin... I've never had a problem using it on any other site, but LinkedIn has firewalls (I think) that make them faulty. The only way I've been able to force a wait is with time.sleep()
Nopes, please don't pull a rug over the real issue through time.sleep(). Can you elaborate what exactly you mean by WebDriverWait doesn't seem to work on linkedin? Update the question with your current code and error stack trace.
When I use WebDriverWait I can actually see it waiting for its condition... then it keeps scripting once you whatever you were waiting for loaded onto the browser. If you use a WebDriverWait on LinkedIn, you can see the error message and the script killed way before your condition ever had a chance to load.
@ChrisBrocious Well I feel like the xpath is not unique. Can you search for the xpath in the HTML if it matches exactly 1 element?
|
0

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

1 Comment

ok, you're gonna have to ask a question and tag me on it to answer. I can't put a block of code into a comment. I'm happy to help though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.