We can know element's enabled/disabled status very easily using isEnabled() method in selenium webdriver software test as described in THIS EXAMPLE POST. Now supposing you have a scenario where you wants to enable any disabled text box or disable any enabled text box during software test execution then how to do it? Webdriver do not have any built in method by which you perform this action. But yes, Webdriver has JavascriptExecutor interface by which we can execute any javascript using executeScript() method.
I have posted many posts on how to execute javascript in selenium webdriver software testing tool to perform different actions. You can view all those example on THIS LINK. Same way, we can enable or disable any element using JavascriptExecutor interface during webdriver software test case execution. To disable text box, we will use html dom setAttribute() method and to enable text box, we will use html dom removeAttribute() method with executeScript() method. Javascript syntax for both of these is as bellow.
document.getElementsByName('fname')[0].setAttribute('disabled', '');
document.getElementsByName('lname')[0].removeAttribute('disabled');
Now let we use both these javascripts practically to understand them better. VIEW THIS POST to see how to enable or disable text box in selenium IDE software testing tool.
@Test
public void test () throws BiffException, IOException, InterruptedException
{
boolean fbefore = driver.findElement(By.xpath("//input[@name='fname']")).isEnabled();
System.out.print("\nBefore : First Name Text box enabled status is : "+fbefore);
boolean lbefore = driver.findElement(By.xpath("//input[@name='lname']")).isEnabled();
System.out.print("\nBefore : Last Name Text box enabled status is : "+lbefore);
Thread.sleep(2000);
//To disable First Name text box
JavascriptExecutor javascript = (JavascriptExecutor) driver;
String todisable = "document.getElementsByName('fname')[0].setAttribute('disabled', '');";
javascript.executeScript(todisable);
Thread.sleep(2000);
//To enable Last Name text box
String toenable = "document.getElementsByName('lname')[0].removeAttribute('disabled');";
javascript.executeScript(toenable);
Thread.sleep(3000);
boolean fafter = driver.findElement(By.xpath("//input[@name='fname']")).isEnabled();
System.out.print("\nAfter : First Name Text box enabled status is : "+fafter);
boolean lafter = driver.findElement(By.xpath("//input[@name='lname']")).isEnabled();
System.out.print("\nAfter : Last Name Text box enabled status is : "+lafter);
}
what is the meaning of ('fname')[0] is it stored inside an array....or when to use [0]. please explain
ReplyDeletegetElementsByName will return the list of elements and from them we have to find first element. that's why used [0].
DeleteHi, can i get javasript methods and properties in eclipse? Please Reply..
ReplyDelete@Archana you may import the below package then you can create an object and then use it
Deleteimport org.openqa.selenium.JavascriptExecutor;
Once above statement is included then
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("return document.domain");
Like this you can run the script
Worked like a charm for me in PowerShell:
ReplyDelete$PasswordInput = FindElement ([By]::XPath('//input[@name="passwordinput"]'))
$Browser.ExecuteScript('arguments[0].removeAttribute("disabled");',$PasswordInput)
$PasswordInput | SendKeys "$($AccountData.P1)" -clear
Thanks was able to implement this successfully smoothly
ReplyDelete