0

I am using the xpath selector for the button itself and selenium gives me an error selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="ember1355"]/button"} when I try to click. What I am trying to do is use selenium to connect with people in linkedin. When I search for a specific term, then I want to pull out all the profiles that say "Connect" and click on the each button. The html is

<div class="search-result__actions">
        <div id="ember3093" class="ember-view">    
            <button aria-label="Connect with Persons name" class="search-result__actions--primary button-secondary-medium m5" data-control-name="srp_profile_actions" data-ember-action="" data-ember-action-3096="3096">Connect</button>   
        <div id="ember3099" class="ember-view"><div id="ember3100" class="ember-view"><!----></div></div>
        </div>
        </div>

and my code is:

if each.find('div', class_='search-result__actions').find('div').text.strip() == "Connect":
    id = each.find('div', class_='search-result__actions').find('div')['id']
    id = '//*[@id="'+id+'"]'
    print(id)
    #this is where the code isn't working
    driver.find_element_by_xpath('//*[@id="ember1355"]/button').click()
2
  • The element to be selected is out of view. I suggest scrolling to element before attempting to click. Commented Oct 2, 2018 at 1:09
  • Is there anyway to scroll to that specific element? Commented Oct 2, 2018 at 2:16

1 Answer 1

1

Try to avoid using Xpath

Try with css_selector:

# Finds the right css selector
button = driver.find_element_by_css_selector('.search-result__actions--primary')
# Clicks the button
driver.execute_script("arguments[0].click();", button)
Sign up to request clarification or add additional context in comments.

8 Comments

Is that a better method? Also, can it click links that are off screen or do I have to navigate to the link on screen?
Css selector performs better then xpath and are easier to read. Your issue above is not related to the element being off screen but an issue in your xpath. Selenium click() method attempt to scroll prior to perform the action of clicking. If you want to scroll before clicking here's how: el = driver.find_element_by_css_selector('#ember1355 .search-result__actions--primary') self.driver.execute_script("arguments[0].scrollIntoView();", el)
Thanks for the insight. However, I tried that code and I am getting the error that the button is not clickable at that point and another element would receive the click. I have tried so many different things but nothing seems to work to make it click on the button. Any ideas?
From your HTML id should be ember3093 not ember1355?<br/> Can you check using this code if the search return an unique element:<br/> el = driver.find_elements_by_css_selector('#ember1355 .search-result__actions--primary')<br/> This code should return a list of element. If there is more then one element we need to be more specific in the value of the locator.
Ember3093 and ember1355 look like different elements on the page specific to the person. LinkedIn change that id for each person and I have my script to scrape that info. When I try to click the button I keep getting the error that that element isn’t clickable and the click goes to an a tag. In the html there isn’t an a tag for the button. It’s weird.
|

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.