How to wrap elements with nokogiri? - ruby

Given a xpath say
Can I do something like:
doc.xpath("/html/body/a").wrap("<span></span>")
And wrap all the links with span tags?

doc.xpath('/html/body/a').each{ |a| a.swap("<span>#{a}</span>") }
found better solution (edit according to #Phrogz)
doc.search('a').wrap('<span/>')

Related

How can define/locate this xpath <g> using this ID?

I'm kinda having a challenge locating this one
I would try something like this: //g[#id="box_0"]
To grab SVG elements using XPath
Please go through : How to use xPath in Selenium WebDriver to grab SVG elements?
In Your case any of the below should work:
//*[contains(#id,'box_0')]
//*[name()='g' and contains(#id,'box_0')]

xpath (//a[contains(., "Test-Test-P24-FA")]) fails to select

This is what I have
1005523 Test-Test-P24-FAID-EUR
I want to write an XPath for the a based on its contained text. I tried the below but it does not work
xpath=(//a[contains(., "Test-Test-P24-FA")])
However, when I try this one, it works.
xpath=(//a[contains(., "Test-Test-P24-FAI")])
Do you have any suggestions?
Thanks!
Try any of this below mentioned xpath.
//a[contains(., 'Test-Test-P24-FAID')]
OR
//a[contains(text(), 'Test-Test-P24-FAID-EUR')]
OR
//a[text()='1005523 Test-Test-P24-FAID-EUR']
Explanation of xpath:- Use text method along with <a> tag.
OR
//a[#class='managed_account_link being_setup'][text()='1005523 Test-Test-P24-FAID-EUR']
Explanation of xpath:- Use class attribute and text method along with <a> tag.
OR
//a[#style='background-color: transparent;'][text()='1005523 Test-Test-P24-FAID-EUR']
Explanation of xpath:- Use style attribute and text method along with <a> tag.

Ruby Nokogiri extract text after the end of a tag

I have a rather basic question here which means i'm probably missing something i'm using Nokogiri to scrape a site.
I want to extract the text AFTER the end of a strong tag within a div which looks like this:
<p style="padding-bottom:0px;"><strong>Location:</strong> Cape Town</p>
Currently my code is as follows:
location = detail_page.css('p[style="padding-bottom:0px;"]').text
Which obviously gives the <strong>Location:</strong> bit as well, is there a way to do this without using a regex?
The reason for asking is that there are other divs in the same format containing information which I need so I can't just delete the strong elements.
Thanks in advance
Marc
You could use XPath:
detail_page.xpath('//p[#style="padding-bottom:0px;"]/strong/following-sibling::text()')
This selects any text nodes that are following siblings of strong elements that are in turn children of p elements with a style attribute witht he value padding-bottom:0px;.
Here I would do as below :
require 'nokogiri'
#doc = Nokogiri::HTML.parse('<p style="padding-bottom:0px;"><strong>Location:</strong> Cape Town</p>')
#doc.at_css('p[style*="padding-bottom:0px;"] > text()').text.strip
# => Cape Town

Cucumber/Capybara: is it possible to mix xpath and css on the same command?

I have an xpath expression that looks like this:
find(:xpath, "//div[#id='drawer-1' and #class='drawer']/h2/a[#class='drawer-toggle']")
I was wondering, is it possible to somehow mix this with css to read something like this?
find("div#drawer-1.drawer/h2/a.drawer-toggle")
Or if this is not possible, is there another way to navigate a DOM with css?
Cheers!
You cannot mix xpath with css. However, in your example, the xpath can be translated to css.
You should be able to do:
find("div#drawer-1.drawer > h2 > a.drawer-toggle")
Note that the "/" are changed to ">". Xpath uses "/" as child selector, where as css uses ">".
A couple useful links:
Child selectors
A cheat sheet that compares xpath with css locators

Get specific element in webdriver containing text

What are some good ways to retrieve a specific element in WebDriver/Selenium2 based only on the text inside the element?
<div class="page">
<ul id="list">
<li>Apple</li>
<li>Orange</li>
<li>Banana</li>
<li>Grape</li>
</ul>
</div>
Essentially, I'd like to write something like this to retrieve the specific element:
#driver.find_element(:id, "list").find_element(:text, "Orange")
This is very similar to how I would use a selector when finding text inside a link (i.e. :link_text or :partial_link_text), but I would like to find elements by text inside normal, non-link elements.
Any suggestions? How do you deal with this issue? (In case you were wondering, I am using Ruby.)
You could do that with xPath. Something like this for your example:
#driver.find_element(:id, "list").find_element(:xpath, './/*[contains(., "Orange")]')
A couple years late, but I was just going to ask this question and answer it so other could find it...
I used a css selector to get all the li elements and then filtered the array based on the text:
#driver.find_elements(css: '#list > li').select {|el| el.text == 'Orange'}.first
You could then .click or .send_keys :return to select the option.

Resources