Wednesday, 8 February 2017

Selenium - Creating Xpath using text of the object

As all knows xpath is differentiated in 2 ways while getting used in selenium, one is absolute and other is relative. Nowadays chrome browser helps user with the option available in the browser to copy the xpath which can be used directly in the script.

But many times the xpath provided by the browser is not correct or the property used in that is dynamic. So we have to write it down. I am providing the below examples which will help you to write down the xpath with correct syntax's and different identifiers also.

Using Text of the Object

If you don't know the html tag of the object.

"//*[text()='text of object']"

If you know the html tag of the object.

"//a[text()='text of object']"

If don't know the whole text but only few character. But make sure it should be unique.

"//*[contains(text(),' character of the text object')]"

"//*[contains(@id,' character of the id')]"



Selenium - How to handle login popups in Chrome browser

I was facing issues while dealing with one of the applications where the login popup is coming to fill the user id and password which is not the web elements to be handled using the selenium webdriver.

I have come across a solution which is very simple to be used in chrome. We just need to append the userid and password with the url of the application and load it into the browser.

 String[] URLText = URL.split("//");
 String URL2 = "//" + UserID + ":" + Password + "@" + URLText[1];
 URL = URLText[0] + URL2;
 driver.get(URL);

I hope it will resolve the issues if any one faces same.

I will share the same for Internet explorer in my next post.