How would I write a selector which searches for an element which contains specific text in any of its children.
This is what I have so far, but alas it doesn't work:
div.taskBlock[contains(., 'Open Activities')]
What about:
.//div[contains(#class, 'taskBlock') and .//*[contains(., 'Open Activities')]]
It finds div that contains class name taskBlock and it has descendants that contain text "Open Activities".
Related
JHow do I grab this text here?
I am trying to grab the text here based on that the href contains "#faq-default".
I tried this first of all but it doesn't grab the text, only the actual href name, which is pointless:
//a/#href[contains(., '#faq-default-2')]
There will be many of these hrefs, such as default-2, default-3 so I need to do some kind of contains query, I'd guess?
You are selecting the #href node value instead of the a element value. So try this instead:
//a[contains(#href, '#faq-default-2')]
Without using index specificity. I'm trying to target an element with exact text, but which also ignores the text of sibling elements. For example, target the span with Save below.
<span>Click and save money!</span>
<span>
<i>Icon</i>
Save
</span>
So something like //span[contains(text(), 'Save')] would grab any span with "Save" in it.
Try the xpath : //span[text()[normalize-space(.)='Save']]
It looks for span elements which have text nodes whose space-trimmed value is exactly Save
I have the following code as part of a test:
if page.text.include? "Provide your details"
go_and_enter_details
end
The webpage has multiple elements labelled h2, including the one that's being checked for content.
This error is returned:
Ambiguous match, found 3 elements matching css "h2" (Capybara::Ambiguous)
How can get Ruby/Cucumber/Capybara to identify whether the page contains the text I need to check?
You have various options:
if the element you want to fill is the first one in the page, you can use first element
first('YOUR ELEMENT').click
you can use within, so you can tell capybara where to find your element (for example, in a div)
within('body > div > div') do
#find your element and do your work
end
you can use the CSS selector or the XPath of your element
I'm looking through HTML documents for the text: "Required". What I need to find is the element that holds the text. For example:
<p>... Required<p>
I would get to element name = p
However, it might not be in a <p> tag. It could be in any kind of tag, which is where this question differs from some of the other search text Stack Overflow questions.
Right now I'm using:
page.at(':contains("Required")')
but this only get me the full HTML element
The problem you have is the :contains pseudo class matches any element that has the searched for text anywhere in its descendants. You need to find the innermost element that contains such text. Since html is the ancestor of all elements, if the page contains the text anywhere then html will contain, and so that will be the first matching element.
I’m not sure you can achieve this with CSS, but you can use XPath like this:
page.at_xpath('//*[text()[contains(., "Required")]]')
This finds the first element node that has a text() node as a child that contains Required. When you have that node (if it exists) you can then call name on it to give the name of the element.
For CSS you can do:
page.at('[text()*="Required"]')
It's not real CSS though, or even a jQuery extra.
You should use CSS selectors:
page.css('p').text
I'm trying to write xpath for one of the elements in a tree-like structure in the UI.
The tree looks like a windows file structure, like parent node, child. So, in order to find child node parent node has to be clicked.
+ [file icon] Book
|_ Book 1
|_ Book 2
|_ Book 3
Selenium gives the following xpath for the text 'Book' in above tree shown
//ul[#id='book_xxx']/li/ul/li[8]/span
when I click on file icon, selenium gave me following
//ul[#id='book_xxx']/li/ul/li[8]/img[1]
How can one write an xpath for clicking the file icon(i.e image) based on knowing the span text? I need the xpath for clicking on file icon image.
It appears that the span and img elements are both children of the 8th li element, and I'm assuming the text inside the span that you would like to match on is "Book".
If you wanted to select the span filtering on the text you should be able to use:
//ul[#id='book_xxx']/li/ul/li[span[text()='Book']]/img[1]
This uses a nested predicate filter to identify the li that has a child span element who's text node value is "Book", and then selects that li's first img element.
Try:
//ul[#id='book_xxx']/li/ul/li[8]/img[1] | //ul[#id='book_xxx']/li/ul/li[8]/span
Which is practically xpath1 or xpath.