paul
paul

Reputation: 4487

Write clickable xpath for entire text in different "span" tags but under one "p" tag

Here's xpath that I wrote but it is of not optimum xpath expression, I think.

//div[contains(@class,'config')]/a[contains(.,'student')]/../p[contains(.,'Never logged as ') and contains(.,'admin')]

HTML is this:

<div class="config">
  <a class="something student something " href="something" htmlelement = "something">
     <div class="bla bla"/>
     <div class="bla bls> xyz </div>
     <div class="bla blab">abc</div>
     <span>Allow it</span>
  </a>
  <p>
     <span>Never logged as</span>
     <span class="b">admin.</span>
     <img src="/images/info.png"/>
  </p>
  <div class="bla bla">qwerty</div>
</div>

Now what I want is to write a generic xpath like this: //div[contains(@class,'config')]/a[not(contains(.,'#{$role_type}'))]/../p[contains(.,'#{$condition}')]

and click on this: @browser.link(:xpath => my_xpath).when_present.click

I am passing only two parameters in above xpath "role_type" and "condition", but when I do that it fails to identify it, because text "admin" is in another span.

So how to collect entire span text? I tried this XPath expression for selecting all text in a given node, and the text of its chldren post too, but here it is giving string as output but I want clickable element not string.

I am using WATIR (Ruby+Cucumber)

Upvotes: 0

Views: 436

Answers (2)

Chuck van der Linden
Chuck van der Linden

Reputation: 6660

If you just want to locate the first link that is next to "Never logged on as admin", then

@browser.p(:text => "Never logged as admin").parent.link.click

Otherwise, an approach such as Justin's answer might be needed.

Upvotes: 0

Justin Ko
Justin Ko

Reputation: 46846

I think you might be out of luck for using XPath as I do not believe there is a string concatenation feature (at least in XPath 1.0).

Instead, I would use other Watir and Ruby methods. For example, the following will go through the config div elements and find the one that contains the role link and condition text. From the found element, you can then click its link.

$role_type = 'student'
$condition = 'Never logged as admin.'

config = browser.divs(class: 'config').find do |div|
    div.link(class: $role_type).exists? && div.p(text: $condition).exists?
end
config.link.click

Upvotes: 1

Related Questions