mayankmehtani
mayankmehtani

Reputation: 435

Selenium Xpath works sometimes, does not work the following time

I'm trying to make an application to automate job applying on linkedin with selenium.

When I find an xpath for an element on the webpage and tell my code to locate the element, it found it and clicked it with no issue.

However when I run the same exact code later on

alpha = driver.find_element_by_xpath("//*[@id='ember2350']/form/button")

I'm greeted with a no such element exception, why does this happen?

Edit: Here's the HTML of the button I'm trying to get to

<button aria-expanded="false" aria-controls="linkedin-features-facet-values" class="search-s-facet__name-wrap search-s-facet__name-wrap--pill button-secondary-medium-muted" data-is-animating-click="true">
  <div class="search-s-facet__name-wrap-container search-s-facet__name-wrap-container--pill">
    <div class="search-s-facet__name">
      <span class="visually-hidden">Expand LinkedIn Features facet</span>
      <h3 class="search-s-facet__name Sans-17px-black-55%-semibold">LinkedIn Features</h3>
    </div>
      <span class="search-s-facet__svg-icon svg-icon-wrap"><li-icon aria-hidden="true" type="caret-filled-down-icon" size="small"><svg viewBox="0 0 24 24" width="24px" height="24px" x="0" y="0" preserveAspectRatio="xMinYMin meet" class="artdeco-icon" focusable="false"><path d="M8.8,10.66L14,5.12A0.07,0.07,0,0,0,13.93,5H2.07A0.07,0.07,0,0,0,2,5.12L7.2,10.66A1.1,1.1,0,0,0,8.8,10.66Z" class="small-icon" style="fill-opacity: 1"></path></svg></li-icon></span>
  </div>
</button>

Upvotes: 0

Views: 530

Answers (2)

Andersson
Andersson

Reputation: 52665

On LinkedIn @id are dynamic values, so you shouldn't use them to locate elements. Try below instead:

driver.find_element_by_xpath("//button[.//span='Expand LinkedIn Features facet']").click()

Upvotes: 1

Pritam Maske
Pritam Maske

Reputation: 2760

It seems that the Id - "ember2350" is dynamic and the numeric is changing so that's why it is not working as it is changing everytime.

Use this xpath :

//span[normalize-space()='Easy Apply']

enter image description here

Upvotes: 0

Related Questions