I am trying to find the relative xpath for the image (please refer the image attached).
Currently I am using the following absolute xpath and it is working fine, but it is too long:
xpath:=//DIV[#id='ResultSection']/DIV[1]/TABLE[1]/TBODY[1]/TR[2]/TD[2]/DIV[1]/DIV[1]/TABLE[1]/TBODY[1]/TR[15]/TD[10]/IMG[1]
You can have multiple Relative Xpaths:
//img[#title='Change']
//img[#class='its value']
//img[#onclick='its value']
Multiple Combinations: //img[#title='Change' and #class='its value']
Related
Is there any difference between relative XPaths and minimal XPaths or both are same?
In Firebug there are two types of XPath mentioned in the options: 'XPath' and 'Minimal XPath'.
The difference between the two options is described within the documentation to the HTML panel.
The option Copy Minimal XPath is meant to make the XPath, which relates to one element, as short as possible. So the word 'minimal' actually refers to the length of the resulting XPath.
It is currently (Firebug 2.x) only available for elements, which have an ID. And for those elements it copies the XPath in the form of
//*[#id="elementID"]
where elementID represents the ID given within the id attribute of the element. So the words 'minimal' and 'relative' actually mean the same at the moment. Though future versions of Firebug may extend the feature to produce minimal XPaths for elements without ID. And those minimal paths don't necessarily have to be relative.
The option Copy XPath is available for all elements and copies an absolute XPath to an element, which e.g. looks like this:
/html/body/div/div[1]/div/div/table[4]/tbody/tr[17]/td[2]/a
What is the difference between absolute and relative xpaths? Which is preferred in Selenium automation testing?
I am preparing test scripts using Selenium and Robot framework.
Absolute Xpath: It uses Complete path from the Root Element to the desire element.
Relative Xpath: You can simply start by referencing the element you want and go from there.
Relative Xpaths are always preferred as they are not the complete paths from the root element. (//html//body). Because in future, if any webelement is added/removed, then the absolute Xpath changes. So Always use Relative Xpaths in your Automation.
Below are Some Links which you can Refer for more Information on them.
difference-between-absolute-path
xpath-tutorial-for-selenium
choosing-effective-xpath
An absolute xpath in HTML DOM starts with /html e.g.
/html/body/div[5]/div[2]/div/div[2]/div[2]/h2[1]
and a relative xpath finds the closed id to the dom element and generates xpath starting from that element e.g.
.//*[#id='answers']/h2[1]/a[1]
You can use firepath (firebug) for generating both types of xpaths
It won't make any difference which xpath you use in selenium, the former may be faster than the later one (but it won't be observable)
Absolute xpaths are prone to more regression as slight change in DOM makes them invalid or refer to a wrong element
Consider Below Html
<html>
<body>
<input type ="text" id="username">
</body>
</html>
so Absoulte path= html/body/input
and Relative path = //*[#id="username"]
Disadvantage with Absolute xpath is maintenance is high if there is nay change made in html it may disturb the entire path and also sometime we need to write long absolute xpaths so relative xpaths are preferred
Absolute XPath:
It is the direct way to find the element, but the disadvantage of the absolute XPath is that if there are any changes made in the path of the element then that XPath gets failed.
The key characteristic of XPath is that it begins with the single forward slash(/) ,which means you can select the element from the root node.
Below is the example of an absolute xpath.
/html/body/div[1]/section/div/div[2]/div/form/div[2]/input[3]
Relative Xpath:
Relative Xpath starts from the middle of HTML DOM structure. It starts with double forward slash (//). It can search elements anywhere on the webpage, means no need to write a long xpath and you can start from the middle of HTML DOM structure. Relative Xpath is always preferred as it is not a complete path from the root element.
Below is the example of a relative XPath.
//input[#name=’email’]
We can say mainly there are 3 differences:
Absolute will start with / where as Relative will start with //
Attributes is not used in absolute where as attribute is used in relative
Absolute is a complete path where as relative is partial path
Mainly Relative is preferred, Absolute is not preferred because since its a complete path and elements can change because of dynamic nature so there may be breakage of path so relative is preferred.
I am facing an issue with FirePath
I am getting
html/body/center/center[3]/form/table/tbody/tr[1]/td[2]/input
from the XPath of the FirePath. This is an absolute path.
But I need a relative path.
How to get it?
Better way is to write your own custom relative xpath.
There is an alternate way to get the relative xpath with help of the FirePath tool also. To get relative XPATH: Click on the drop down menu on the Firepath button and Unselect ‘Generate absolute XPath’.
Then you should be getting relative XPATH (remove the "." from generated XPATH) as shown below:
I would like to search for a link on a page by its domain name - possibly using contains()? And then only show the anchor text of that link.
I've been able to get all of the a tag using
//a[contains(text(), 'domain_name')]
but unable to retrieve just the anchor text. Can anybody help?
Just use the text() node:
//a[contains(#href, 'domain_name')]/text()
http://www.google.com/search?q=youtube
Trying to get the url part www.youtube.com/ of the search
using this xpath but there is no output from it.
.//*[#id='rso']/li[1]/div/div[2]/div[1]/cite
i've also tried using the css path same issue
CSS: div[aria-label='Result details']+div>div cite.
Btw, your xpath works fine for me. If you use selenium to retrieve your text for example, you should write xpath=.// in this case, because it recognize selector as xpath by preceding // symbols. Also //*[#id='rso']/li[1]/div/div[2]/div[1]/cite//text() will return three textNodes www., youtube and .com/