PinPiguin
PinPiguin

Reputation: 477

Selenium invalid selector: Unable to locate an element with the xpath expression error

I am failing to select the span than contains Full Name:

No matter what selector I use, I always get the error:

Message: invalid selector: Unable to locate an element with the xpath expression

For example I have tried:

xpath = "//a[@class='app-aware-link']/span[@dir='ltr']/span[@aria-hidden='true']"
xpath = "//a[@class='app-aware-link']/span[@dir='ltr']"

Also full xpath that are valid in console $x(xpath), not valid with Selenium

<div class="display-flex">
   <span class="entity-result__title-line entity-result__title-line--2-lines">
      <span class="entity-result__title-text
         t-16">
         <a class="app-aware-link " href="https://www.linkedin.com/in/inge-de-grauwe-6ab2a98?miniProfileUrn=urn%3Ali%3Afs_miniProfile%3AACoAAAF3gqMBfw45cjhzEVAqNU943iCK0siy3fU" data-test-app-aware-link="">
            <span dir="ltr">
               <span aria-hidden="true">
                  <!---->Inge De Grauwe<!---->
               </span>
               <span class="visually-hidden">
                  <!---->View Inge De Grauwe’s profile<!---->
               </span>
            </span>
         </a>
         <span class="entity-result__badge t-14 t-normal t-black--light">
            <div class="display-flex
               flex-row-reverse
               align-items-baseline">
               <!---->    
               <span class="image-text-lockup__text entity-result__badge-text">
                  <span aria-hidden="true">
                     <!---->• 1st<!---->
                  </span>
                  <span class="visually-hidden">
                     <!---->1st degree connection<!---->
                  </span>
               </span>
            </div>
         </span>
      </span>
   </span>
   <span aria-hidden="true" class="entity-result__badge-overflow align-self-flex-end t-14 t-normal t-black--light flex-shrink-zero
      ">
      <div class="display-flex
         flex-row-reverse
         align-items-baseline">
         <!---->    
         <span class="image-text-lockup__text entity-result__badge-text">
            <span aria-hidden="true">
               <!---->• 1st<!---->
            </span>
            <span class="visually-hidden">
               <!---->1st degree connection<!---->
            </span>
         </span>
      </div>
   </span>
</div>

If I use

xpath = "//a[contains(@class,'app-aware- 
link')]/span[@dir='ltr']/span[@aria-hidden='true']"

The error still:

Unable to locate an element with the xpath expression driver.find_elements(By.XPATH, '//a[contains(@class,'app-aware-link')]/span[@dir='ltr']/span[@aria-hidden='true']') because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string 'driver.find_elements(By.XPATH, '//a[contains(@class,'app-aware-link')]/span[@dir='ltr']/span[@aria-hidden='true']')' is not a valid XPath expression.

Upvotes: 1

Views: 954

Answers (1)

Prophet
Prophet

Reputation: 33361

Your problem is with first part of the XPath //a[@class='app-aware-link']. The value of class attribute there is "app-aware-link " with a space after "app-aware-link". That's why "//a[@class='app-aware-link']/span[@dir='ltr']/span[@aria-hidden='true']" doesn't match nothing there.
So, to make your code working you can change the XPath in 2 ways:

xpath = "//a[@class='app-aware-link ']/span[@dir='ltr']/span[@aria-hidden='true']"

Or

xpath = "//a[contains(@class,'app-aware-link')]/span[@dir='ltr']/span[@aria-hidden='true']"

Upvotes: 1

Related Questions