1

Given the following Html:

<button type="button"><b><em><u>Next</u></em></b></button>

In the page source I have more than one button. I have only option to find this element with text Next. I tried following methods but without success:

  1. driver.findElement(By.cssSelector("button>b>em>u[text()='Next']"))
  2. driver.findElement(By.cssSelector("button>b>em>u[.='Next']"))
  3. driver.findElement(By.cssSelector("button>b>em>u.contains('Next')"))
StuartLC
  • 104,537
  • 17
  • 209
  • 285
virendra chaudhary
  • 179
  • 1
  • 2
  • 18

4 Answers4

1

It might be the case that the button has whitespace or other characters, I would switch to an xpath selector.

Selecting the u element

This xpath will select the u element:

driver.findElement(By.xpath("//button/b/em/u[contains(., 'Next')]"))

Selecting the button

To select the button containing the above u element, e.g. so that the button can be clicked:

driver.findElement(By.xpath("//button[b/em/u[contains(., 'Next')]]"))

Xsl fiddle of this here

Element not Found?

As an aside, and in general, when looking at HTML to determine css or xpath selectors for Se, ensure that you are looking at the final rendered version of Html, and not just the original Html served from the web server - client side javascript may have modified the html significantly after loading, and also note that if the served html was not well formed, that browsers can also change html. In Chrome, I use the Developer tools : Elements pane to view html.

Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285
  • StuartLC, I am getting error. he xpath expression '//button[b/em/u[contains(., 'Next')]]' cannot be evaluated or does notresult in a WebElement – virendra chaudhary Dec 29 '14 at 16:05
  • At a guess there may be more than one button with the word `Next` on it (possibly hidden?). `driver.findElement(By.xpath("(//button[b/em/u[contains(., 'Next')]])[1]"));` should find just the first. – StuartLC Dec 29 '14 at 16:11
  • @virendrachaudhary Glad you found the issue - care to share? Might help future users :) – StuartLC Dec 29 '14 at 17:03
  • Thank you so much Stuart, following solution worked for me. driver.findElement(By.xpath("//button/b/em/u[contains(., 'Next')]")) – virendra chaudhary Dec 29 '14 at 17:03
0

Try assigning an ID to the button and then map the functionality to the button ID rather than button text like this: <button id ="btn1" type=button">

gamers542
  • 25
  • 4
0

You are combining xpath selector syntax in your css selector. Try this in stead:

driver.findElement(By.xpath("//button/b/em/u[text()='Next']"))

or

driver.findElement(By.xpath("//u[text()='Next']")).

If you do wish to use a css selector, remove the text() part like so:

driver.findElement(By.cssSelector("button>b>em>u"))

joostschouten
  • 3,863
  • 1
  • 18
  • 31
0

Below xpath should find 1st occurrence of "Next" button:

//button[text()='Next'][1] 

If you want to get 2nd occurrence:

//button[text()='Next'][2] 
Surya
  • 4,446
  • 2
  • 17
  • 19