Reputation: 501
The HTML code:
<a id="id47" title="textA"
onclick="var wcall=wicketAjaxGet('?wicket:interface=:1blablabla')"
href="#">
<span class="icon" style="background-image: url('folder/background.png')"></span>
<span>textB</span>
I want to use Selenium webdriver to locate and click the icon above. Normally, I would do this using the ID, but unfortunately, the program keeps changing these IDs for no apparent reason, so I have to locate the element using either the title (textA) or Span (textB)
I came up with the following:
public void performclick() throws Throwable {
Thread.sleep(5000);
driver.findElement(By.xpath("//span[contains(text('textB')]")).click();
}
or
public void performclick() throws Throwable {
Thread.sleep(5000);
driver.findElement(By.xpath("//*[contains(text('textA')]")).click();
}
and any deviations on these. Everytime I run the scenario, it gives nothing back. What am I not figuring right?
Upvotes: 0
Views: 819
Reputation: 7008
You can try cssSelectors as well,
css = a[title='textA']
or
If some part of your id remains constant,for instance,in your case if id47
changes to id48
or id200
and so on...you can use contains(,i.e, *) or starts with (,i.e, ^) :
css = a[id*='id'][title='textA']
or
css = a[id^='id'][title='textA']
For span (textB),the following would work :
css = span.icon + span
Here + is used to locate following sibling.
Upvotes: 1