I want to click the parent of an element if the text within the child matches a certain string. I am using contains to find the text and :xpath,"../../" to find the parent. Here is ruby code:

 [email protected]_element(:xpath,"//*[contains(.,'602384710')]")
puts test
 puts test.find_element(:xpath, "../..").click

The HTML snippet of the element I want to target is:

<a class="tv-card-link" href="/my-account/tv/overview?ban=7AhDHZ0ZYFVI1m8B2HmvrQ&tv_instance_id=_A5Xvou4fk-WsIWSnSkG2A">

    <div class="tv-card-wrapper">
        <div class="tv-card-icon"></div>
        <div class="tv-card-details">
            <div class="tv-card-id">

From the code, I want to click on the <a> if I find the text 602384710. When I was testing, the puts test prints out the address of Selenium WebElement so I know that works. However, selenium is unable to find the parent of the element. The error I get is:

Unable to locate element: {"method":"xpath","selector":"../.."} (Selenium::WebDriver::Error::NoSuchElementError)
[remote server] file:///C:/Users/X169804/AppData/Local/Temp/webdriver-profile20150407-7120-6gmcur/extensions/fxdriver@go
oglecode.com/components/driver-component.js:10271:in `FirefoxDriver.prototype.findElementInternal_'
[remote server] file:///C:/Users/X169804/AppData/Local/Temp/webdriver-profile20150407-7120-6gmcur/extensions/fxdriver@go
oglecode.com/components/driver-component.js:603:in `fxdriver.Timer.prototype.setTimeout/<.notify'
./features/step_definitions/ss-login_step.rb:26:in `/^I click on plan number (\d+)$/'
features\ss-login.feature:16:in `Then I click on plan number 602384710'

I think I may be missing a simple thing. Any help appreciated :)

3 Answers 3


You have two options.

If you're using findElement relative to another WebElement, your XPath needs to start with a dot (the element as your starting point). See the accepted answer in this topic.

Alternatively, the XPath contains goes looking in child nodes too. You could try to immediately target the link element like so: "//a[@class='tv-card-link' and contains(.,'602384710')]"

  • Hey! Thank you so much for your answer. I used the . as in test.find_element(:xpath, "./../..").click but that gives me the same error. Apr 7, 2015 at 19:39
  • I think that your first XPath is wrong, why do you use the * symbol? This will likely return the topmost html tag (because the html itself also contains that value). Do you have a link to this page for testing purposes?
    – FDM
    Apr 8, 2015 at 5:19

I have tested this XPath online. Please try:


Just a wild guess without any testing, but shouldn't it just be:


The problem is that when searching with the results of a find_element it cannot go back up the tree anymore, from then on it will only search within the already found results.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.