How to access element using Watir and XPath - 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.

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

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

How to write the xpath for span tag

<span class="autoSuggKeywords">
samsung
refrigerator
</span>
i want to access the content refrigerator using xpath
objective :identify the element using Autosuggestion
i tried below
.//span[#class="autoSuggKeywords"]
.//[text()='refrigerator']
Try following solution :
//span[contains(#class, 'autoSuggKeywords') and text() = 'refrigerator']
Hope it will help you.
xpath expression:
//span[#class="autoSuggKeywords" and contains(.,'refrigerator')]

xpath - Get href attribute

I am trying to get all href links via xpath from the following page:
href page
I tried the following:
//div[#class='article-tile__images']/a[#class='article-tile__link js-article-tile__link acte-article-catalogName-lnk']
Any suggestions what I am doing wrong?
I appreciate your replies!
Working with class attributes is much easier, readable and concise in CSS selectors:
a.article-tile__link
which matches 65 links when I issue $$('a.article-tile__link') in the Chrome console.

selenium findElement by another thing than By.id

I'm quite new to this wonderfull tool that selenium is, and i'm trying to make some examples tests in my web app (html/JS).
I managed to select some (most) elements withtheir id with the command driver.findElement(By.id("elementId"));
but i'm unable to find some elements that do not have an id tag.
I tried these following lines without result, as i have have an
By.cssSelector("//img[#alt='smthg']")
By.xpath("//img[#src='path/to/img'")
a mix of the two aboce (alt and src in xpath and cssSelector
This element HTML code is
<img src="absolut/path/to/img.png" border="0" onclick="JSfunction(0)" alt="smthg" style="cursor: pointer;">
If somebody could help me, that would be very nice :)
Thanks, and have a good day !
You can use either of below
By.cssSelector("img[alt='smthg'][src*='path/to/img']");
or
By.xpath("//img[#alt='smthg' and contains(#src,'path/to/img')]")

Resources