1

I am trying to find the xpath of the element below, so that I can later get the text using Ruby Selenium-webdriver (ie. helloPage.mainHeader.get_text).

<div class="container">
  <div class="template-section">
    <div class="front">
      <h3 class="containerHeading">
      <i class="icon_image"></i>
      "Hello world   <-----------------------3 whitespaces
      "
      </h3>
    </div>
   </div>
</div>

I've worked on xpaths but everytime I rerun the test it timesout essentially the element does not exist. It is clearly visible on the UI and not hidden.

Why is my xpath is wrong? I have tried the following:

  1. //div[@class='container']//div[@class='template-section']//div[@class='front']//h3[@class='containerHeading']
  2. //div[@class='front']//h3[@class='containerHeading']
  3. //h3[@class='containerHeading']

I did put sleep prior to executing helloPage.mainHeader.get_text, where mainHeader has the XPath expression, and that didn't work. Is there something mysterious about the Hello World text? The format is indeed like the way I typed it out.

7
  • Looking at the xpaths you have provided they are all valid. However, for 'A' you could rewrite it as //div[@class='container']/div[@class='template-section']/div[@class='front']/h3[@class='containerHeading'], note the single fore-slashes separating the elements. So are you seeing an error or exception when you try to find the element? Have you used another tool to check that your xpath is valid as well? Commented Sep 10, 2013 at 7:59
  • Use Firefox with firepath to test your xpath. Also, just as an experiment, change [@class='foo'] to [contains(@class, 'foo')]. Commented Sep 10, 2013 at 8:04
  • @Mark Rowlands - Yes, I am getting errors when I used your provided xpath. Instead, I change it to: //div[@class='container']//div[@class='template-section']/div[@class='front']/h3‌​[@class='containerHeading'] , then it is a valid xpath. Note that there are // on template-section The value of the xpath returns blank when I try to get the text. Commented Sep 10, 2013 at 16:24
  • @RobbieWareham - Yes, I have been using Chrome extensions (xPath Viewer, and XPath Helper) to check things. As an experiment I did try: //div[contains(@class, 'container')]/div[contains(@class,'template-section')]/div[contains(@class, 'front')]/h3[contains(@class, 'containerHeading')] As a result, I am still getting nothing back as text Commented Sep 10, 2013 at 16:25
  • You had to include the double-fore-slash? Strange. That notation means 'find any of the following'. So you're, in effect, saying "find me any div with the class 'container', and in that find me any div with the class 'template-section', and inside that fine me any div with the class 'front'... etc". Perhaps this is a Ruby thing? Sorry, I'm a Python user. As asked before, have you tried any tools to help validate/build your 'xpath'? Are the 'elements' you want to interact with inside an 'iframe' perhaps? Commented Sep 10, 2013 at 16:29

1 Answer 1

0

all your xpaths seems correct to me... I think when you are trying to find the element using your xpath ... the element is not loaded properly... try to use explicit wait. Please try to use the code provided below:

    wait = Selenium::WebDriver::Wait.new(:timeout => 10)
    wait.until { driver.find_elements(:xpath, "Any of your above mentioned xpaths") }

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

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.