user3356141
user3356141

Reputation: 501

Can't find a specific tag using Selenium Webdriver

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

Answers (2)

Amith
Amith

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

paul trmbrth
paul trmbrth

Reputation: 20748

Try:

//*[@title='textA']

or

//*[text()='textB']

Upvotes: 0

Related Questions