0

How can I access the element using xpath selector if that particular element does not have any attributes?

<?xml version="1.0" encoding="UTF-8"?>
<challenge-details-card>
   <div class="card-body card-content-container">
      <div>
         <h1 class="card-title text-clr-tertiary">Custom Name</h1>
         <hr class="border-clr-gainsboro" />
         <h3 class="card-info text-clr-tertiary">
            <span translate="Challenges.ChallengeRuns" ng-reflect-translate="Challenges.ChallengeRu">Challenge runs:</span>
            July 15 - July 27
            <!--bindings={"ng-reflect-ng-if": "true"}-->
            <span>
               (12 days
               <span translate="Genesis.GlobalLabels.Left" ng-reflect-translate="Genesis.GlobalLabels.Left">Left</span>
               !)
            </span>
         </h3>
         <p class="card-copy">Custom Text</p>
      </div>
   </div>
   <div class="card-footer background-clr-light-gray">
      <a translate="Genesis.Challenges.ViewChallengeRules" ng-reflect-translate="Genesis.Challenges.ViewChallen">View Challenge Rules</a>
   </div>
</challenge-details-card>

http://prntscr.com/ofnudx

I am trying to create an xpath to get "July 15 - July 27" text.

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
a92
  • 1
  • 1

3 Answers3

1

You can access innerText() property of the h3 tag using XPath text() operator in conjunction with contains() function, something like:

//h3[contains(text(),'July 15 - July 27')]

Demo:

enter image description here

References:

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
0

The text is present under h3 tag.

You can try with this css selector :

h3.card-info.text-clr-tertiary

If you want xpath :

//h3[contains(@class,'card-info text-clr-tertiary')]

You can use getText() method to get the text which is present under selenium-Java library.

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
0

To extract the text July 15 - July 27 you can use the following Locator Strategy:

  • Java and xpath:

    System.out.println((String)((JavaScriptExecutor)driver).executeScript("return arguments[0].childNodes[2].textContent;", new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(., 'Challenge runs')]/..")))));
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352