12

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();
}
2
  • What error are you getting? Commented Mar 3, 2014 at 13:40
  • Element is not currently visible and so may not be interacted with. Same as before. I think it might be the program not responding to the correct code though, not the solution given... Commented Mar 3, 2014 at 14:22

5 Answers 5

5

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

Sign up to request clarification or add additional context in comments.

6 Comments

Doesn't help either unfortunately... Concerning the sleep, I considered that aswell, but the waitforelementpresent results in a ridiculous piece of code. We also have a DoD which states that a page needs to be loaded within 3 seconds...
Ahh, just realised, you need to change the xpath to; //span[contains(@class, 'ie-fallback-marker') and normalize-space()='words']. Also I am interested as to why waiting makes a 'ridiculous piece of code'?
Strangly enough, That did the trick! Thanks a million! Just curious on one thing: 'words' doesn't have any whites, so why does the element need to 'remove whites'?
It is surrounded by white space
actually, that's a typo... There are no spaces around the real code, but somehow your solution does the trick :)
|
3

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.

1 Comment

I have to automate Hippo CMS. It has a basic map structure on the left side. Clicking on a certain directory opens a list of documents on the main frame. in THAT frame, I want selenium to click a certain type of document. When I inspect the element when the script stops, it doesn't show the clickable link, but it does show it on the screen. I believe Hippo CMS is being generated by javascript and widgets, which are just.so.awefull to work with.
1

Try using this:

driver.findElement(By.xPath("//span[starts-with(., \"" + document + "\")]")).click();

It worked for me! In case you want to check some text in the middle, try:

driver.findElement(By.xPath("//span[contains(., \"" + document + "\")]")).click();

1 Comment

Element is not currently visible and so may not be interacted with. Unfortunately, it doesn't work :/
1

I see no reasons why following xpath wouldn't work

driver.findElement(By.xpath("//span[contains(text(), \"" + document + "\")]"));

Also, you probably need your input string to be constant

//string document is words.
   public void testscenario123(final String document)      throws Throwable {

If you were to get ElementNotVisibleException then check if your locator is returning you the correct element, perhaps there is another <span> element present containing the text in the document variable.

1 Comment

Well, it does work on all other pages, so this is a fix :) thanks for that. Now I just need to click something that is hidden, which Ill try and research right now.
-2

We can use xPath to find such an element, because the text is inside the span tag. Below is the code snippet...

WebElement elmnt=driver.findElement(By.xpath(".//*[@id='MainLayout']/div/div[2]/div/div/div[3]/div/div[2]/div/div/div/div/div[2]/div/div/div[2]/div/div[2]/div/div/div[3]/div/div[2]/div/div/div[1]/div/div/div/div/div/div[4]/span/span"));

elmnt.click(); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.