1. To HighLighting Element
Syntax: JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement ele = driver.findElement(By.id("123"));
js.executeScript("arguments[0].style.border='2px solid red' " , ele);
//js.executeScript("Script", Arguments);
Example:
class JavaScript
{
public static void main(String[] args)
{
WebDriver driver = new ChromeDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
driver.get("https://testingcolleges.blogspot.com/p/testingform.html");
WebElement src = driver.findElement(By.id("mail"));
js.executeScript("arguments[0].style.border='2px solid red' " , src);
}
}
Output:
2. Disable a Web Element: To disable a web element we use the following method.
Syntax: JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement ele = driver.findElement(By.id("123");
js.executeScript("arguments[0].setAttribute('disabled',' ');" , ele);
//js.executeScript("Script", Arguments);
Syntax 2: JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('123').setAttribute('disabled',' ');" );
Example:
class JavaScript
{
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver" , "path");
WebDriver driver = new ChromeDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
driver.get("https://testingcolleges.blogspot.com/p/testingform.html");
WebElement FirstName = driver.findElement(By.name("fname"));
js.executeScript("arguments[0].setAttribute('disabled', ' ');" , FirstName);
}
}
3. Disable to Enable: To enable, the disabled web element we use the following method.
Syntax: JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement ele = driver.findElement(By.id("123");
js.executeScript("arguments[0].removeAttribute('disabled',' ');" , ele);
//js.executeScript("Script", Arguments);
Syntax 2: JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('123').removeAttribute('disabled',' ');" );
Example:
class JavaScript
{
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver" , "path");
WebDriver driver = new ChromeDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
driver.get("https://testingcolleges.blogspot.com/p/testingform.html");
//to disable
WebElement FirstName = driver.findElement(By.name("fname"));
js.executeScript("arguments[0].setAttribute('disabled', ' ');" , FirstName);
//to enable
js.executeScript("arguments[0].removeAttribute('disabled', ' ');" , FirstName);
}
}
No comments:
Post a Comment