user11759165
user11759165

Reputation:

How to click on all connect button in linkedin with python & selenium?

I made linked in connecting script but he is clicking on my class button now my error is how to click on all connect button?

how can I click on all connect button?

here is my code:

#search

click_search = WebDriverWait(driver, 50).until(EC.element_to_be_clickable((By.XPATH, "/html[1]/body[1]/header[1]/div[1]/form[1]/div[1]/div[1]/div[1]/artdeco-typeahead-deprecated[1]/artdeco-typeahead-deprecated-input[1]/input[1]"))).send_keys("pyt")
time.sleep(.2)
click_search = WebDriverWait(driver, 50).until(EC.element_to_be_clickable((By.XPATH, "/html[1]/body[1]/header[1]/div[1]/form[1]/div[1]/div[1]/div[1]/artdeco-typeahead-deprecated[1]/artdeco-typeahead-deprecated-input[1]/input[1]"))).send_keys("hon")
time.sleep(.2)
click_search = WebDriverWait(driver, 50).until(EC.element_to_be_clickable((By.XPATH, "/html[1]/body[1]/header[1]/div[1]/form[1]/div[1]/div[1]/div[1]/artdeco-typeahead-deprecated[1]/artdeco-typeahead-deprecated-input[1]/input[1]"))).send_keys(Keys.ENTER)
click_people = WebDriverWait(driver, 50).until(EC.element_to_be_clickable((By.XPATH, "/html[1]/body[1]/div[5]/div[7]/div[4]/div[1]/div[1]/header[1]/div[1]/div[1]/div[1]/ul[1]/li[1]/button[1]/span[1]"))).click()
click_connect = WebDriverWait(driver, 50).until(EC.element_to_be_clickable((By.XPATH, "/html[1]/body[1]/div[5]/div[7]/div[4]/div[1]/div[2]/div[1]/div[2]/div[1]/div[1]/div[1]/div[1]/ul[1]/li[4]/div[1]/div[1]/div[3]/div[1]/button[1]"))).click()
click_done = WebDriverWait(driver, 50).until(EC.element_to_be_clickable((By.XPATH, "/html[1]/body[1]/div[5]/div[8]/div[1]/div[1]/div[1]/section[1]/div[1]/div[2]/button[2]"))).click()

Upvotes: 0

Views: 797

Answers (1)

Dmitri T
Dmitri T

Reputation: 168147

Using absolute XPath locators is not the best idea as it makes them very fragile and sensitive to any DOM change

The recommended locator strategy is using ID where possible, however when IDs are absent or dynamic it's better to come up with an alternative way.

For example you can stick to button text.

Compare your XPath expression for People:

/html[1]/body[1]/div[5]/div[7]/div[4]/div[1]/div[1]/header[1]/div[1]/div[1]/div[1]/ul[1]/li[1]/button[1]/span[1]

with this one:

//span[text()='People']

And both expressions are basically matching the same element:

enter image description here

So you can fetch all Connect buttons using find_elements_by_xpath() function like:

connect_buttons = driver.find_elements_by_xpath("//button[text()='Connect']")

for connect_button in connect_buttons:
    print(connect_button.get_attribute("aria-label"))

References:

Upvotes: 1

Related Questions