Pages

Wednesday, 7 March 2012

Why am getting security message while instantiating IE WebDriver?

Security Message:“Protected Mode must be set to the same value (enabled or disabled) for all zones.”


Solutions 1:


Set manually the protected mode to the same value (enabled or disabled) for all zones in ‘Internet Options’ —> ‘Security’ Tab.


Solution 2:


Set a capability while instantiating IE driver, as shown below:


 DesiredCapabilities capability=DesiredCapabilities.internetExplorer();
capability.setCapability(
InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_
IGNORING_SECURITY_DOMAINS, true);
WebDriver webdriver = new InternetExplorerDriver(capability);

How can I Kill the WebDriver instance?

The quit() method will kill the WebDriver instance.


That is,


//If "driver" is an instance of WebDriver;Then to kill the instance:
driver.quit();

How can I read background-color of an element?

We can get/read the background-color of an element by using getCssValue() method of WebDriver:


driver.get("http://www.google.co.in/");
String color = driver.findElement(By.name("btnK")).getCssValue("background-color");

System.out.println("The background color of Google search button"+color);

How can I get required attribute value of the element?

We can get the specific attribute of an element by using getAttribute() method:


//Find the element
WebElement gButton = driver.findElement(By.name("btnK"));
// Get the required attribute value
String gButtonStyle = gButton.getAttribute("style");

System.out.println("The Google search button's style attribute value is "+gButtonStyle);

The getAttribute() method gets the value of an element attribute specified.


Suppose, you want to get the value/content of a text field then, specify the attribute name as “value” (instead off “style”).

How can I get the text of the element?

Using getText() method, we can get the text of the element.


That is,


WebElement gButton = driver.findElement(By.name("btnK"));
String gButtonText = gButton.getText();
System.out.println("The Google search button's display text is "+gButtonText);

The getText() method gets the text of an element. This uses either the textContent (Mozilla-like browsers) or the innerText (IE-like browsers) of the element, which is the rendered text shown to the user.

How can I get title of the window?

We can get the title of the window by using getTitle() method:


String title = driver.getTitle(); 
System.out.println("Title of the page is "+title);

Monday, 5 March 2012

How can I move backwards or forwards in browser's history?

We can move backwards and forwards in browser’s history as below:


//To go backward
driver.navigate().back();
//To go forward
driver.navigate().forward();