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

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')]

Related

Watir, locate element by custom attribute

I am using Watir 6.16 and I have come across this line of code
<div data-guid="SearchTitle" class="acme"></div>
Not sure how to locate such an element in Watir, I have tried this -
element(:search_title, custom_attribute: "SearchTitle")
But this returns nothing, so am I forced to use xpath or is there another way?
Kev
You can change that - into _, it would work. Look at the code below
browser.div(data_guid: 'SearchTitle')

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.

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

How to wrap elements with nokogiri?

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/>')

How to access element using Watir and XPath

I have some HTML that looks like this:
<h1 id="header">Header</h1>
I would like to click it using Watir and XPath.
After watir-webdriver 0.5.1 selecting random element with an xpath was updated to:
browser.element(:xpath => "//h1[#id='header']").click
thanks to:
https://groups.google.com/forum/#!topic/watir-general/c6Orvy7Qalw
browser.element_by_xpath("//h1[#id='header']").click
Sources:
http://wiki.openqa.org/display/WTR/XPath
http://zeljkofilipin.com/2007/07/03/find-element-by-xpath/
browser.h1(:xpath, "//h1[#id='header']").click
Also not XPath, but works:
browser.h1(:html, /header/).click
Not using XPath, but it works:
browser.h1(:id, "header").click
Another example using xpath here:
browser.element xpath: "//div/cite[contains(.,'some text')]/ancestor::div[#class='rc']/h3/a"
Checkout this simple framework that I uploaded to Github:
https://github.com/atfuentess/watir_cucumber_automation/
The stack used is: watir/cucumber/rspec
Perhaps it can help someone.

Resources