Reputation: 718
I have a HTML File hierarchy in tree structure in a web page as shown in picture.
The HTML code is
<div class="rtMid rtSelected">
< span class="rtSp"/>
< img class="rtImg" alt="Automation" src="http://192.168.1.6/eprint_prod_3.8/images/StoreImages/close_folder.png"/>
< span class="rtIn" title="Automation">Automation (1)</span>
</div>
In Selenium WebDriver is there a way to click on the Automation (1)
link by searching only the text I don't want to use XPath reason is the location will be changing so is there a way to find it by its text and click on it.
Upvotes: 2
Views: 1373
Reputation: 350
2 Approaches
Approach 1:
By Class Name:
Here we are having class Name for the Text Automation (1) that is rtIn. Perform driver.findElement(By.className("rtIn")).click();
Approach 2:
By CSS Selector of Parent and Class Name
CSS Selector of Parent:.rtSelected
WebElement element1 = driver.findElement(By.cssSelector(".rtSelected")) element1.className("rtIn").click();
Approach 3:
By Direct CSS Selector: 1. .rtIn 2. .rtSelected > .rtIn
It is better to use the second CSS Selector
driver.findElement(By.cssSelector(".rtSelected > .rtIn")).click();
Upvotes: 0
Reputation: 718
searching by title worked well
driver.findElement(By.xpath("//span[contains(@title,'Automation')]")).click();
Upvotes: 0
Reputation: 32845
XPath is powerful, you found it's unreliable you are not using it right. Spend some time at XPath Tutorial please.
This is a simple solution to your question, but there could be many other things you need to think about. E.g. matching title and text, etc.
driver.findElement(By.xpath(".//span[text()='Automation (1)']")).click();
CSS selector is also powerful and faster, more readable than XPath. But in your case, it doesn't support find by text.
Upvotes: 2