Hi all,
Here I am going to list out some rare and useful tips for selenium users.
1. Start/Shutdown SeleniumServer in proper way
OR you want to shoudown via Browser, use below command
http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer
2. To Scroll by postion
selenium.getEval("window.scrollBy(0,200)");
--- Here 0 and 200 are x and y postions to scroll
3. Get No of CheckBoxes Checked/Unchecked
4. Drop-down lists handling
To get list items:
String[] items = selenium.getSelectOptions("name=listbox");
To set or select a item on dropdown:
selenium.select("listbox", "label=item1"); //For Simple Select List
There is a select list, which contains a text box and down arrow image button. Clicking on button image, opens a drop down which contains element with dynamic id's.
Dynamic elements are : drop down elements (div's) , and image button.
This can be done by
So, I have used text function as,
5. HTTP to HTTPS problems
http://qaselenium.blogspot.com/2011/04/how-to-handle-https-and-security-popups.html
6. Locate Elements by position
If you have 2 buttons on a page. Both have same ID, type, and available attributes are same. How you will identify which element to click?. Here is the answer.
7. XPath Type : Functions
Always we have chances to meet locator problems while elements with same attributes, ids, names, etc. These kind of scenarios can be solved by using below techniques. Normally we can use 4 ways to identify the elements.
1. String
2. Boolean
3. Number
4. Node-Set
Here they are,
Here I am going to list out some rare and useful tips for selenium users.
1. Start/Shutdown SeleniumServer in proper way
- try {
- serverSocket.close();
- //Server not up, start it
- try {
- RemoteControlConfiguration rcc = new RemoteControlConfiguration();
- rcc.setPort(RemoteControlConfiguration.DEFAULT_PORT);
- server = new SeleniumServer(false, rcc);
- e.printStackTrace();
- }
- try {
- server.start();
- e.printStackTrace();
- }
- }
- }
- selenium.stop();
- if(seleniumServer != null) {
- try {
- selenium.shutDownSeleniumServer();
- seleniumServer .stop();
- seleniumServer = null;
- e.printStackTrace();
- }
- }
- }
OR you want to shoudown via Browser, use below command
http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer
2. To Scroll by postion
selenium.getEval("window.scrollBy(0,200)");
--- Here 0 and 200 are x and y postions to scroll
3. Get No of CheckBoxes Checked/Unchecked
- selenium.getXpathCount("//input[@type='checkbox' and @checked]");
- selenium.getXpathCount("//input[@type='checkbox' and not(@checked)]");
Also You can use like
css=input[type=\"checkbox\"]:checked
4. Drop-down lists handling
To get selected value:
- selenium.isElementPresent("//select[@name='listbox']/option[contains(text(),'item2')]")
- selenium.getSelectedValue("name=listbox");
To get list items:
String[] items = selenium.getSelectOptions("name=listbox");
To set or select a item on dropdown:
selenium.select("listbox", "label=item1"); //For Simple Select List
There is a select list, which contains a text box and down arrow image button. Clicking on button image, opens a drop down which contains element with dynamic id's.
Dynamic elements are : drop down elements (div's) , and image button.
This can be done by
selenium.click("//input[@id='listbox']/following-sibling::img");
This clicks on image. Now we need to select the dropdown list element, which also has dynamic id.So, I have used text function as,
selenium.click("//div[text()='item1']"); //which clicks on "item1" in the drop-down list.
Also have a look at this link Select an option from an ExtJS ComboBox5. HTTP to HTTPS problems
http://qaselenium.blogspot.com/2011/04/how-to-handle-https-and-security-popups.html
6. Locate Elements by position
If you have 2 buttons on a page. Both have same ID, type, and available attributes are same. How you will identify which element to click?. Here is the answer.
- //input[@id='button' and position()=1] OR //input[@id='button'] //1st button
- //input[@id='button' and position()=2] //2nd button
7. XPath Type : Functions
Always we have chances to meet locator problems while elements with same attributes, ids, names, etc. These kind of scenarios can be solved by using below techniques. Normally we can use 4 ways to identify the elements.
1. String
2. Boolean
3. Number
4. Node-Set
Here they are,
- Node-Set: last(), position(), count(), id(), local-name(), namespace-uri(), name()
- String: string(), concat(), starts-with(), contains(), substring-before(), substring-after(), substring(), string-length(), normalize-space(), translate()
- Boolean: boolean(), not(), true(), false(), lang()
- Number: number(), sum(), floor(), ceiling(), round()
- selenium.click("xpath=(//input[@type='checkbox'])[last()]");
- selenium.click("xpath=(//input[@type='submit'])[last()-1]");
- selenium.click("xpath=(//input[@type='checkbox'])[position()=2]");
ReplyDeleteThanks, this is generally helpful.
Still, I followed step-by-step your method in this selenium online training
selenium certification
selenium online courses
learn selenium online