Html:
<div class="component-multi-row-slide-title"> Cloth </div>
The xpath
/html/body/div/main/div/div/div[2]/div/div/div/div[1]/div/div***[4]***/div/div[2] the div[4]
keep changing. If new item come into web, developer will change the div number.
I want to click on 'Cloth'. The xpath keep changing when new category is added.
To start with using absolute xpath have certain disadvantages, as when new elements are added the xpath of the previously identified elements will keep on changing. Hence the solution would be to use Relative Xpath.
As per the HTML you have provided, to click on the element with text as Cloth you can use the following Locator Strategy:
XPath A:
//div[#class='component-multi-row-slide-title' and contains(., 'Cloth')]
XPath B:
//div[#class='component-multi-row-slide-title' and normalize-space()='Cloth']
Absolute Xpath: It contains the complete path from the Root Element to the desire element.
Relative Xpath: This is more like starting simply by referencing the element you want and go from the particular location.
You use always the Relative Path for testing of an element. The reason behind that is if you make any architectural change in the website the change won't effect the testing or selecting of the element.
So Use relative xpath which is as follows
WebElement cloth = driver.findElement(By.xpath("//div[normalize-space()='Cloth']"));
cloth.click();
Let me know if this doesn't help
I would recommend to use customized xpath. As given below:
driver.findElement(By.xpath("//div[contains(text(), 'Cloth')]").click
driver.findElement(By.xpath("//div[contains(text(), 'Fashion Acessories')]").click
It will work for sure. Feel free to let me know if something is wrong regarding xpath.
Thanks
You need to use relative xpath in this case.
You can use any xpath from the below options. The first two will find the element by text and the 3rd option by class.
1>>driver.findElement(By.xpath("//div[contains(text(),'Cloth')]")).click();
2>>driver.findElement(By.xpath("//*[contains(text(),'Cloth')]")).click();
3>>driver.findElement(By.xpath("//div[#class='component-multi-row-slide-title']")).click();
Let me know if the same class name exist for other elements also.
Just for you reference: XPath is used to find the location of any element on a webpage using HTML DOM structure.
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.
So, in that case Relative Xpaths can be used:
//div[contains(text(),'Cloth')] or //*[contains(text(),'Cloth')]
//div[contains(text(),'Fashion Acessories')] or //*[contains(text(),'Fashion
Acessories')]
How to use:
driver.findElement(By.xpath("//div[contains(text(),'Cloth')]"));
You write the following code
driver.findElement(By.xpath("//div[normalize-space()='Cloth']").click
The above code will identify the element directly and also it would not be brittle in nature when developer changes the page elements.
Related
There are few elements in an UI page without ID. I will download a particular version and then save all the current element tags and give to script as input, since few elements are not having id, this is causing script failures.
How can I locate the element without using Xpath.
Is there any simple way when there is no fixed id.
The short answer is "no." Sorry. All of the usual ways (by id, class, etc.) are relying on the same css information to locate elements. Xpath just shows all the ugly plumbing out in public. I don't think xpath has ever been described as "simple" but there is usually a way, using xpath, to find any element.
Xpath can be intimidating. Start with a plugin that will generate the xpath for you, once you click on an element. Usually the xpath generated will be extremely long and inefficient, but with practice you can see what can be trimmed and what is crucial. And to do that, also use a plugin that will "check" your xpath to see if it can find the element. Once you can find it (and ONLY the element you want) try trimming it to see if you can still find it with the abbreviated xpath locator.
reference ImageDon't be afraid of Xpaths. It's relatively easy to grab an Xpath using the Google Chrome browser. Navigate to your page and open Developer tools. Right-click on the particular tag for which you need an Xpath. Copy -> Xpath
I am trying to locate and element on my web page, but the element i want to locate has no unique Id's or anything of that matter. I have only a dynamically changing class name.
I tried using Absolute Xpath but its too brittle.
<div class="frca889813467244cd88b375adbb9452c2s4" style="left:274.05px;top:141.75px;width:94.5px;height:18.9px;"><div
class="frca889813467244cd88b375adbb9452c2s11">6000.00</div></div>
From above DOM i want to get the value every time i locate the above element (Here its 6000.00 but might change in other tests).
I will suggest going with absolute XPath, I know you mentioned you have a hard time creating absolute XPath. I guess below note can help
Note:- if you copy XPath from Firefox it will mostly give you absolute XPath whereas chrome on another side will give relative
or the other way is to make an XPath using another stable element in the tree
I hope this workout for yours. if you can share a link or inspect element snapshot showing Full DOM I can help you even better. Thanks :-)
Thanks in advance!
I want to know if there is any way in CSS selector in which we can find nth element in page?
I believe in xpath if there multiple element which satisfies an xpath we can get nth element using below syntax:
//input[2] ' Select 2nd Input
Please note I am not looking for nth-child(n) or nth-of-type(n) which selects child element of parent.
Please note I am not looking for nth-child(n) or nth-of-type(n) which selects child element of parent.
That is how you locate the Nth element in CSS locators. If your looking for the Nth root element, you can use html or body as parent elements. By.CssLocator("body:nth(2)")
That seems like a strange solution though. Can you post more information describing your requirements? There may be a better way of locating the element than using a CSS locator. Of course if you have the ability to modify the source, than this could easily be fixed by design. I recommend using data-* attributes.
I am trying to learn Selenium and am trying to write the xpath locator for the "About Us" link on the web page - www.hdfc.com
I can do it with link as:
link=About Us
I have tried the following and it works fine:
xpath=//a[text()='About Us']
but I wanted to write the locator using xpath axes so that its flexible enough. Can someone please point me in the right direction?
In the case you have put forward the best selector you could use is ID, this is because IDs (much like classnames etc) are not dependent on the structure of the document at all but more about the content or purpose of the element. in this case you would want something like:
driver.findElements(By.Id("ic-aboutUs");
Another thing you should be aware of in general is that xpath expressions are considered a worse way to identify your elements than the use of CSS selectors, especially if you are testing in IE as the xpath implementation there is not native and is very slow. I suggest reading over http://saucelabs.com/resources/selenium/css-selectors for a brief look at some examples and also maybe have aread of http://saucelabs.com/resources/selenium/selenium-xpath-marks-the-spot in order to see some of the negatives of using xpath.
Use the below xpaths to detect 'About Us'link in your web page
Below xpath was written by refering the immediate parent node
//li[#class='expanded']/child::span[text()='About Us']
This xpath was written by using the parent node of Menu bar(parent of whole menu items)
//ul[#class='menu hdfc-investor']/child::li[contains(.,'About Us')]
I have tried to locate button in my web app using xpath but it changes automatically each time I open selenium IDE. Is there any other way to locate it except using xpath or position? can I locate it using class name? If yes then how can I do it?
You can use xpath to find element by class name.
//*[#class='someClass']
where, someClass is the class name of your element.
Since it is your webapp, consider adding an id or a name to uniquely identify the element. It also makes the xpaths easier to write as you don't need to consider the possibility where you might be grabbing too many elements.
Answer - If by default recorded xpath are not working for your application, then you can define your own xpath for those components which should remain same throughout execution.
Please refer below URL which shows ways to develop userdefined xpath :-
http://docs.seleniumhq.org/docs/appendix_locating_techniques.jsp
Use a CSS selector. This site really helped me: http://saucelabs.com/resources/selenium/css-selectors
if it has an id on it you can just say "id=yourid"
for css it could be something like this: "css=button[class*='yourclass']" <-- that says it's a button, and that in class it contains yourclass.