9

I'm trying to add contacts on LinkedIn using Python and Selenium. I'm attempting to do so by adding the contact suggestions made by LinkedIn in the "Network" tab (https://www.linkedin.com/mynetwork), which has an infinite scroll feature.

Basically I want the script to locate the button "Connect", which is next to each suggested profile, click the button, and then repeat until error whereby the script should scroll down to load more "Connect" buttons to reiterate.

The best way I've found to locate the button element is by find_element_by_class_name() since all the connect buttons have the same class. I've also tried locating the elements using CSS and Xpath, without success.

PROBLEM: The script is able to click the first Connect button, but none after that. I've tried many ideas for implementation (locating by Xpath, CSS, using a list of buttons to click), yet none seem to work. Below is the relevant part of the script.

while True:
    try:
        driver.find_element_by_class_name("mn-person-card__person-btn-ext.button-secondary-medium").click()
        time.sleep(1)
    except:
        pass
        print("trying to scroll")
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        time.sleep(1) 

Any ideas? To me it seems as if the code should work, and as if there is something else which is preventing success. Maybe a bug or similar. Might mention that I'm rather new to all of this, and it's the first script I'm trying to make to manipulate a browser.

I'm using Firefox driver. Full script can be found here: http://pastebin.com/qtdNsRtz

Thanks in advance!

1
  • You can't use compound classes with by_class_name(). You are using a CSS selector in there and that's likely why it's not working. Commented Mar 26, 2017 at 1:48

3 Answers 3

14

You should use find_elements for finding all elements with same class Try this to get all elements:

elements = driver.find_elements_by_class_name("mn-person-card__person-btn-ext.button-secondary-medium")

then use a for loop to click each of them. For example:

for e in elements:
    e.click()
Sign up to request clarification or add additional context in comments.

3 Comments

How is a for loop going to work when a while loop doesn't?
Perfect! I had already tried that solution, but when reading your answer I realized that I was attempting to click the elements by (following your notation): for e in elements: browser.e.click() Your comment made me realize that the "browser" addition was not necessary. Of course, it should instead be as you suggested: for e in elements: e.click() Now it works perfectly! Thanks a lot!
What if elements in an empty list?
8

The way you are trying to use find_element_by_class_name locator is not correct as this locator doesn't support compound classes within.

You need to use either xpath or cssSelector if class attribute have more then one class name :

driver.find_element_by_xpath("//button[@class='mn-person-card__person-btn-ext button-secondary-medium']").click()

Comments

0

This worked for me.

from selenium.webdriver.common.by import By
find_element(By.CLASS_NAME, "class name")

Comments

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.