How we can select the second last checkbox and third last checkbox. Selenium Notes, Examples, Videos, By.www.laxmiroy.blogspot.com/
Node Set : position()
If you want to select any object based on their position using xpath then you can use position() function in xpath.
Sometimes an element gets identfied by a value that could be surrounded by other text, then contains function can be used.
We can use last()- function to indentify the last object among all similar objects.
Below code will check or uncheck the second last checkbox and thrid last checkbox respectively.
selenium.click("xpath=(//input[@type='submit'])[last()-1]");
selenium.click("xpath=(//input[@type='submit'])[last()-2]");
You want to select second checkbox and forth checkbox then use below command
selenium.click("xpath=(//input[@type='checkbox'])[position()=2]");
selenium.click("xpath=(//input[@type='checkbox'])[position()=4]");
above code will select second and forth checkbox respectively.
String : starts-with()
Many web sites create dynamic element on their web pages where Ids of the elements gets generated dynamically.
Each time id gets generated differently. So to handle this situation we use some JavaScript functions.
XPath: //button[starts-with(@id, 'continue-')]
To demonstrate, the element can be located based on the ‘suggest’ class without having
to couple it with the ‘top’ and ‘business’ classes using the following
XPath: //input[contains(@class, 'suggest')].
Example: How to click on link on the page which has many links with same name and attributes.
Below is the example of your html which has 3 links with same name and same attributes
<html>
<body>
<a href="http://www.google.com" name="a1">Link</a>
<a href="http://www.yahoo.com" name="a1">Link</a>
<a href="http://www.gmail.com" name="a1">Link</a>
</body>
</html>
If you want to click on first link then use below command
<tr>
<td>clickAndWait</td>
<td>xpath=(//a[@name='a1'])[position()=1]</td>
<td></td>
</tr>
If you want to click on second link then use below command
<tr>
<td>clickAndWait</td>
<td>xpath=(//a[@name='a1'])[position()=2]</td>
<td></td>
</tr>
If you want to click on last link or third link then use below command
<tr>
<td>clickAndWait</td>
<td>xpath=(//a[@name='a1'])[last()]</td>
<td></td>
</tr>
OR
<tr>
<td>clickAndWait</td>
<td>xpath=(//a[@name='a1'])[position()=3]</td>
<td></td>
</tr>
Selenium RC.
Click on first link
selenium.click("xpath=(//a[@name='a1'])[position()=1]");
selenium.waitForPageToLoad("80000");
Click on second link
selenium.click("xpath=(//a[@name='a1'])[position()=2]");
selenium.waitForPageToLoad("80000");
Click on last link
selenium.click("xpath=(//a[@name='a1'])[last()]");
selenium.waitForPageToLoad("80000");
Click on thrid link
selenium.click("xpath=(//a[@name='a1'])[position()=3]");
selenium.waitForPageToLoad("80000");
No comments:
Post a Comment