0

I am trying to click on the following button on a linkedin page using selenium:

<button id="ember607" class="share-actions__primary-action artdeco-button artdeco-button--2 artdeco-button--primary ember-view" data-control-name="share.post"><!---->
<span class="artdeco-button__text">
    
        Post
    
</span></button>

I have tried to use:

  • driver.find_element_by_id, but the id of the button seems to keep changing number
  • driver.find_element_by_xpath, but this contains the button number, so also fails
  • driver.find_element_by_class_name('share-actions__primary-action artdeco-button artdeco-button--2 artdeco-button--primary ember-view'), this fails even though the class name is correct ?

Basically, all methods generate the same error message:

Exception has occurred: NoSuchElementException
Message: no such element: Unable to locate element:{[*the_error_is_here*]}

I have also tried the xpath contains() method, but this does not find the button.

What would be the correct way to click on this button please ?

I am using python version 3.9 on windows with driver = webdriver.Chrome

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
D.L
  • 4,339
  • 5
  • 22
  • 45

5 Answers5

1

Sometimes there are problems with buttons that are not clickable at the moment. Try this:

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By


wait = WebDriverWait(driver, 10)

button = wait.until(EC.element_to_be_clickable((By.XPATH, '[YOUR X_PATH TO THE BUTTON]')))
driver.execute_script("arguments[0].click()", button)

It's not the cleanest way to click any Button with selenium, but for me this method works mostly everytime.

Lukas Scholz
  • 622
  • 5
  • 11
  • 1
    I upvoted this because whilst the answer was not the accepted answer, it was helpful in getting the solution, which used similar techniques `expected_conditions`, `WebDriverWait` and `By`. – D.L Dec 06 '20 at 12:00
1

The element is an Ember.js enabled element. So to click() on the element with text as Post you can use either of the following Locator Strategies:

  • Using css_selector:

    driver.find_element_by_css_selector("button.share-actions__primary-action[data-control-name='share.post']>span.artdeco-button__text").click()
    
  • Using xpath:

    driver.find_element_by_xpath("//button[contains(@class, 'share-actions__primary-action') and @data-control-name='share.post']/span[@class='artdeco-button__text' and contains(., 'Post')]").click()
    

Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.share-actions__primary-action[data-control-name='share.post']>span.artdeco-button__text"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(@class, 'share-actions__primary-action') and @data-control-name='share.post']/span[contains(., 'Post')]"))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

References

You can find a couple of relevant detailed discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    this works: `WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(@class, 'share-actions__primary-action') and @data-control-name='share.post']/span[contains(., 'Post')]"))).click()` with the imports as above. Thanks – D.L Dec 05 '20 at 20:50
  • 1
    you are welcome, please upvote the question if you think it is helpful to others. – D.L Dec 05 '20 at 23:33
0
//button[@class="share-actions__primary-action artdeco-button artdeco-button--2 artdeco-button--primary ember-view"]. 

Or

//button[contains(@id,'ember')]
PDHide
  • 18,113
  • 2
  • 31
  • 46
  • do you mean put this into xpath like this: `driver.find_element_by_xpath('//button[@class="share-actions__primary-action artdeco-button artdeco-button--2 artdeco-button--primary ember-view"]')`. If so this returns the same error message. – D.L Dec 05 '20 at 19:11
  • Cannot use the `//button[contains(@id,'ember')]` suggestion as there are *many* buttons wth `ember`+`some_random_number`. – D.L Dec 05 '20 at 19:14
  • Try using explicit wait with the first xpath then – PDHide Dec 05 '20 at 19:40
0

By xpath this should work:

//button/span[contains(text(), "Post")]

Combine it with a wait for the element:

button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, "//button/span[contains(text(), "Post")]"))
    )

The problem with your by class selectors is the multiple class names. See this question: How to get elements with multiple classes for more details on how to overcome that.

DMart
  • 2,401
  • 1
  • 14
  • 19
  • `Message: no such element: Unable to locate element: {"method":"xpath","selector":"//button/span[contains(text(), 'Post')]"}`.... – D.L Dec 05 '20 at 19:50
  • and does it work if you add a wait (i've edited answer) – DMart Dec 05 '20 at 19:52
  • no, it gives a different error: `Exception has occurred: TypeError __init__() takes 2 positional arguments but 3 were given`. note: i corrected one set of single quotes to double quotes. – D.L Dec 05 '20 at 20:24
  • I had a typo. Updated for you. – DMart Dec 05 '20 at 20:32
  • still gives `Exception has occurred: TypeError __init__() takes 2 positional arguments but 3 were given` – D.L Dec 05 '20 at 20:40
  • Sorry, I think i need more context to where that error is happening – DMart Dec 05 '20 at 22:37
0

Find the span with Post and click it's button tag.

//span[contains(text(), 'Post')]/parent::button
Arundeep Chohan
  • 9,779
  • 5
  • 15
  • 32
  • i do this `driver.find_element_by_xpath("//span[contains(text(), 'Post')]/parent::button").click()` but get this error `Exception has occurred: NoSuchElementException Message: no such element: Unable to locate element: {"method":"xpath","selector":"//span[contains(text(), 'Post')]/parent::button"}` – D.L Dec 05 '20 at 19:53