Reputation: 7
I am in a situation where there are no unique id and there are number of div
's under a class. Cssselector and xpath
's are so generic that they are not being recognized.
This is what the Html looks like:
This is my code which doesn't work:
@Test
public void NaviToEpisode(){
driver.findElement(By.linkText("/episode")).click();
title_episode = driver.getTitle();
Assert.assertTrue(title_episode.contains("File uploading"));
}
Please help!
Upvotes: 0
Views: 71
Reputation: 788
<div id="links" . . >
seems to be static I hope it's unique as well. Following css selector can be used to select first link (i.e. /episodes/)
#links div:nth-child(1) a
Similarly you can use css selectors to select sub-sequent elements. For example to select 2nd element:
#links div:nth-child(2) a
So instead of using By.linkText("/episode")
, use By.cssSelector("#links div:nth-child(1) a")
.
Upvotes: 0
Reputation: 424
You can use cssSelector, in your case it would be:
driver.findElement(By.cssSelector("#links>div>a").click();
If you use Firefox, install Firebug plugin, then right click on the element you wish to inspect and in menu click on "Inspect with Firebug", once the snippet of your code highlighted right click on it and you should see an option to copy xpath or css.
Upvotes: 1
Reputation: 134
try this driver.findElement(By.xpath("//*[contains(@href, '/episode/')]")).click();
Upvotes: 0