'How to create xpath locator when element does not have any attributes
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>
I am trying to create an xpath to get "July 15 - July 27" text.
Solution 1:[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:
References:
Solution 2:[2]
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.
Solution 3:[3]
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')]/..")))));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Dmitri T |
Solution 2 | cruisepandey |
Solution 3 | undetected Selenium |