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

5 Answers 5

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:

Sign up to request clarification or add additional context in comments.

2 Comments

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
you are welcome, please upvote the question if you think it is helpful to others.
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.

1 Comment

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.
0
//button[@class="share-actions__primary-action artdeco-button artdeco-button--2 artdeco-button--primary ember-view"]. 

Or

//button[contains(@id,'ember')]

3 Comments

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.
Cannot use the //button[contains(@id,'ember')] suggestion as there are many buttons wth ember+some_random_number.
Try using explicit wait with the first xpath then
0

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

//span[contains(text(), 'Post')]/parent::button

1 Comment

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"}
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.

6 Comments

Message: no such element: Unable to locate element: {"method":"xpath","selector":"//button/span[contains(text(), 'Post')]"}....
and does it work if you add a wait (i've edited answer)
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.
I had a typo. Updated for you.
still gives Exception has occurred: TypeError __init__() takes 2 positional arguments but 3 were given
|

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.