Now we know how to identify html elements using only selenium or execute javascript in selenium,
Now let’s see how we can perform actions on different web elements like
textbox – how we can enter text
Button – click
How to get attribute of a html element…
For input buttons
//js.executeScript("return document.getElementsByName('commit')[0].click();")
//or
//element = (WebElement) js.executeScript("return document.getElementsByName('commit')[0];");
//((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
//or
//element.click();
For textbox
WebElement element = null;
//js.executeScript("return document.getElementsByName('uname')[0].value='myName';");
//or
element = (WebElement) js.executeScript("return document.getElementsByClassName('EnterText')[0];");
element.sendKeys("myName");
For Radiobuttons
JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("window.location = 'http://www.qavalidation.com/demo'"); //select 4th radiobutton js.executeScript("document.querySelector("input[value='four']").checked=true;"); List RadBtns = driver.findElements(By.cssSelector("div[name ='experience'] input")); //System.out.println(RadBtns.size()); for(WebElement Rad : RadBtns) { //System.out.println(Rad.getAttribute("checked")); if(Rad.getAttribute("checked")!=null) { System.out.println(Rad.getAttribute("value") + " is checked"); break; } } driver.quit(); } }
For CheckBoxes
Working with CheckBoxes is same as Radiobuttons, just one difference, we can check multiple options in checkboxes,
[Try yourself and let me know if get any issues]
1 Comment