1

Still new to selenium and css/xpath locators. I stumbled across a problem where CSS works but the equivalent XPath doesn't, and I'd really like to know why. I'm using Scala in the examples but it's still the normal Java Selenium2 library. I also use the FirefoxDriver

Here's the interesting part of the HTML:

...
<li class="k-item k-filter-item k-state-default k-last" role="menuitem" style="z-index: auto;">
  ...
  <form class="k-filter-menu k-secondary">
    <div>
      <div class="k-filter-help-text">Show items with value that:</div>
      <span class="k-widget k-dropdown k-header" style="" unselectable="on" role="listbox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-owns="" aria-disabled="false" aria-readonly="false" aria-busy="false">
      <span class="k-widget k-datetimepicker k-header" style="">
      <div>
        <button class="k-button" type="submit">Filter</button>
        <button class="k-button" type="reset">Clear</button>
      </div>
    </div>
  </form>
</li>
...

I'm getting the li with

val filter = driver.findElement(By.cssSelector("li.k-filter-item"))

which works for me.

Then, I want to find the button. It's a dynamic menu thingy sliding out and what not, so I need to wait for it to appear:

new WebDriverWait(driver, selectorTimeout).until(
  new ExpectedCondition[Boolean] {
    override def apply(d: WebDriver) = {
      filter.findElement(By.cssSelector("button[type=submit]")).isDisplayed
    }
  })

And that works nicely, too. My question is, why doesn't the xpath equivalent work:

new WebDriverWait(driver, selectorTimeout).until(
  new ExpectedCondition[Boolean] {
    override def apply(d: WebDriver) = {
      filter.findElement(By.xpath("//button[@type='submit']")).isDisplayed
    }
  })

Anybody?

[EDIT]
Selenium version: 2.35.0
FireFox driver: 2.35.0

I will try it with Opera now.

4
  • Any error did you get? Can you try //button[contains(@type,'submit')] ? Commented Sep 27, 2013 at 12:17
  • Wow. Have you tried it in any other browser than FF? This should definitely work. While it's obviously better to use CSS selector, I understand your confusion. This baffles me, too. Which Selenium version is this? And which Firefox? In firefox, both CSS selector and XPath support should be native. Huh. Commented Sep 27, 2013 at 12:18
  • You'll need the . in front of the XPath too I think: .//button[@type='submit']. Also try a more elaborate one: .//descendant::button[@type='submit']. (Not tested these, but it's a guess). Commented Sep 27, 2013 at 13:29
  • @Arran: you're right! Please turn your comment into an answer an I shall accept it! Commented Sep 27, 2013 at 14:04

1 Answer 1

1

You'll need the . in front of the XPath selector anyway, so that it'll search the current element's descendants/children:

.//button[@type='submit']

Sometimes, a more elaborate XPath can also help:

.//descendant::button[@type='submit']
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.