HTML code looks like this:
<td id="id26a" class="doclisting-name link" style="width: 319px; min-width: 319px;">
<span id="id26b" title="Document">
<span class="ie-fallback-marker">
words
</span></span></td>
I cannot search for Element ID, as it changes all the time. I cannot search for the element's class, as there are multiples which could change locations.
I want to be able to click on "WORDS" in between the SPAN tags. Is this at all possible?
This is what I used so far, but neither seems to work:
//string document is words.
public void testscenario123(String document) throws Throwable {
Thread.sleep(3000);
driver.findElement(By.linkText(document)).click();
}
or
//string document is words.
public void testscenario124(String document) throws Throwable {
Thread.sleep(3000);
driver.findElement(By.xpath("//*[contains(@span,'"+document+"')]")).click();
}
You can select xpath via text and normalize-space will strip white space:
//string document is words.
public void testscenario124(String document) throws Throwable {
Thread.sleep(3000);
driver.findElement(By.xpath("//*[normalize-space()='"+document+"']")).click();
}
You may also wish to consider waiting for the element to appear instead of declaring an explicit sleep
I suspect the element is taking more than 3 seconds to appear.Hence the following exception is thrown.
Element is not currently visible and so may not be interacted with
To avoid this exception implicit or explicit wait must be used.
new WebDriverWait(driver,10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(., \"" + document + "\")]"))).click();
As RobbieWareham has mentioned,even i'm interested to know more about this waiting makes a ridiculous piece of code?
Through Selenium Webdriver,we are just trying to imitate how a user would work.Even he/she has to wait for the element to appear to perform some operation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With