Handling button using JSE:
Handling a button using JavaScript Executor (JSE) in Selenium is similar to handling a checkbox. You can use the executeScript method to interact with the button directly through the DOM.
public class BtnHandlingByJSE {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");
// //open chrome browser
WebDriver driver = new ChromeDriver();
driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/ALL%20Web%20objects.html");
// click 'MySubmit' button using id
// driver.findElement(By.id("MyButton")).click();// assume that is not working
/// JSE
JavascriptExecutor jse = (JavascriptExecutor)driver;
// click 'MySubmit' button using id using JSE
// JS code use id --> document.getElementById('MyButton').click();
// . use the above code, call executeScript();
jse.executeScript("document.getElementById('MyButton').click();");
// get button 'type' attribute val i.e submit
// driver.findElement(By.id("MyButton")).getAttribute("type");// submit
//JS code use id : document.getElementById('MyButton').type;
// HW use above cose , call executescript ()
Object oType = jse.executeScript("return document.getElementById('MyButton').type;");
System.out.println("oType="+oType);
//HW get 'id' value of My submit button i.e MyButton using JSE
// Ans : MyButton
// JS code:
// Hw get 'value' property of My submit button i.e 'My Submit' using JSE
// ans: 'My Submit'
// JS code -->
// HW click 'login' button using JSE
}
}
No comments:
Post a Comment