how to locate specific node for Xpath on index basis? - selenium

I do need to locate second node, on basic of same class available in the Table.
There are six results found on webpage, for query : //td[#class='checkboxCollumn']
If I locate it for second node, By using //td[#class='checkboxCollumn'][2] its giving 0 result.
I have tried some other variations : //td[#class='checkboxCollumn']//i[2] and so on its giving 0 result.
What is the correct way to locate it with index ?

You need to locate like this
(//td[#class='checkboxCollumn'])[2]

Try:
(//td[#class='checkboxCollumn'])[2]
With this:
//td[#class='checkboxCollumn'][2] you trying to match second element within the same container

Use this.. to locate second node...
(//td[#class='checkboxCollumn'])[position()=2]
Here position() is 1-indexed i.e. position will start counting from 1

try using the
(//td[#class='checkboxCollumn'])[2]
as [] has higher priority over //.So rememeber to put the expression is brackets when need to specify the exact node of the selected node-list.
In ur case,it will search for all the elements in the document,that are at the second place.
Even though above thing not work,let me know.

Related

How to locate the first element in selenium

I have a question,
I have this xpath that return 9 results
//nz-select[(#formcontrolname='deviceType')]
I want to locate the first element, so I tried in the xpath the following
//nz-select[(#formcontrolname='deviceType')][1]
//nz-select[1][(#formcontrolname='deviceType')]
however in the F12 console it always 9 results. how can I locate the [1] results, without putting all in list and other issues.
Just want hard coded the results of the first one
regards
To get the first item Try this xpath.
(//nz-select[#formcontrolname='deviceType'])[1]
Please try below code:
(.//nz-select[(#formcontrolname='deviceType')])[1]

How to Find an xpath with some string contained and some not contained in the div?(Xpath not contains)

**Problem :** How to Find an xpath with some string contained and some not contained in the div?(Xpath not contains)?
**Below are three examples**
1)<div>You are now connected to Customer Care Virtual Assistant.</div>
2)<div>You are now connected to sumit.</div>
3)<div>You are now connected to dev.</div>
I have tried *//*[contains(text(),'You are now')]*
this xpath as well but it gives me 3 results and want only to fetch for values which does not contain virtual
i am currently using this xpath - *//*[text()[contains(.,'You are now ') and not[contains(.,'Virtual')]]*
It's not working for me , let me know what mistake i am doing here.
Any help will be appreciated ,
Thanks
You were close.Try the following xpath.
//*[contains(.,'You are now') and not(contains(.,'Virtual'))]
Try another solution
//a[not(contains(text(), 'Virtual')) and contains(.,'You are now')]
or
//a[not(contains(text(), 'Virtual')) and contains(text(),'You are now')]
maybe you can use:
1- use "starts-with" function.
div[starts-with(#id,'something')]
2- use "contains" function.
div[contains(#id,'something')]
or
3- use full xpath, that id is not necessary to use attributes
/html/body/div[3]/div[2]/div[2]/div[2]/div[2]/div[2]/div[2]/div/div[4]/div[1]/div/div/div/div[1]/div/div/div/div[1]/div[2]/div[1]/form[1]/input[1]

In Selenium, why do I get a different result when I use ByChained with two ByXPath and when I use just a ByXPath locator?

I don't know why I am getting a certain element when using By.Chained.
I have the following HTML:
When I use the locator
By.XPath("//*[text()='End date']/..//input[#type='text']")
I get the second input box, as expected:
But, when I use the locator
By.Chained([By.XPath: //*[text()='End date']/..,By.XPath: //input[#type='text']])
I get the first input box:
Why is this happening?
Your second expression (as all starting with /) is an absolute location path. If this By.Chained applies each result from the first expression as context for the second one, you will need a relative expression like:
.//input[#type='text']
Or a more clear one:
descendant::input[#type='text']

How to use array of XPath in robotframework

I have html table which has some one column and N rows. I want to traverse through the array. Since HTML table does not have unique locator. I am using #class attribute to get the rows This is my xpath which gives me matching rows.
//td[contains(#class,'td-entity-field-super-parent')].
When i add index to it like this:
(//td[contains(#class,'td-entity-field-super-parent')])[3],
it works fine in firebox/seleniumIDE.
However with RobotFramework, it works only for first row.
Works:
//td[contains(#class,'td-entity-field-super-parent')][1]
Does not work:
//td[contains(#class,'td-entity-field-super-parent')][2]
//td[contains(#class,'td-entity-field-super-parent')][3]
//td[contains(#class,'td-entity-field-super-parent')][4]
BTW, enclosing entire element in round bracket does not work.
(//td[contains(#class,'td-entity-field-super-parent')])[4]
Can someone please help me here.
Ok. Here is the answer. Simply use "xpath=" as prefix to your xpath. Here is the example
xpath=(//td[contains(#class,'td-entity-field-super-parent')])[4]

Select an nth matched node from a list of matching nodes

I'm working with selenium to perform some automation and I'm attempting to interact with my webpage using Selenium & CSS selectors.
My question is how do I select the nth matched node returned from a list of all matching nodes?
For example my CSS selector is ".contactName" which returns 2 matching nodes. Using Selenium I want to do something like
selenium.Click("css=.contactName the second match");
Any help is greatly appreciated.
This is what I ended up using in order to select the second input with the class name
selenium.Click("xpath=(//input[#class='contactName'])[2]");
Do these two nodes share the same parent? If so, you can try one of these, depending on where they are under their parent in the DOM and whether there are any other kinds of elements:
selenium.Click("css=.contactName:nth-child(2)");
selenium.Click("css=.contactName + .contactName");
selenium.Click("css=.contactName ~ .contactName");
If these two nodes don't share the same parent, you'll probably have to go with an XPath locator instead of CSS:
selenium.Click("xpath=//*[#class='contactName'][2]");