Suggested post to read:
Selenium WebDriver uses pre-defined locators like id, class, Xpath, name, link etc. to locate the elements on webpage. So, in some cases, it becomes difficult to locate element or sometimes you may face consistency problem in locating element due to webpage code.
Many Situations occur in your project where code written with selenium – java (any language binding) does not work (at all or effectively).
There would be few tricky scenarios where just Webdriver object is not sufficient enough or optimum enough to handle with java for e.g. in few of my applications, the web page rendered is very long, and while doing operations with elements, I got errors. So, I used JavaScript in my script to handle the case. Some other cases where JavaScript comes handy is handling pop ups and alerts.
In another case, dropdown wasn’t working for me (it was not properly coded by using select class). It was selecting the elements with are currently visible, wasn’t searching in to the whole list. So, in this case, I need to keep scrolling down continuously & at the same time keep checking If the required option is available to select & is getting selected.
So, there are certain advantages involved when we use the two (java, JavaScript) judiciously in automation scripts. Both the languages are great in their own space.
What are the advantages of JavaScriptExecutr in Selenium?
js.executeScript(“history.go(0)”);
String sText = js.executeScript(“return document.title;”).toString();
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element);
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollBy(0,50)");
js.executeScript("window.scrollBy(0,document.body.scrollHeight)");
JavascriptExecutr js = (JavascriptExecutr)driver;
js.executeScript("window.location = ‘URL-TO-BE-NAVIGATED’");
package basicsOfSelenium; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class JavaScriptExample { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); //Navigate to google.com using javascript JavascriptExecutor js = (JavascriptExecutor)driver; js.executeScript("window.location = 'https://www.google.co.in'"); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.manage().window().maximize(); //Enter the text in textbox using javascript js.executeScript("document.getElementById('lst-ib').value = 'Text to be searched';"); //Click on the button using javascript WebElement element = driver.findElement(By.xpath("//*[@id='tsf']/div[2]/div[3]/center/input[1]")); js.executeScript("arguments[0].click();", element); //Get page Title using JavaScript String PageTitle = js.executeScript("return document.title;").toString(); System.out.println("Page title is: "+PageTitle); //To Refresh the browser using JavaScript js.executeScript("history.go(0)"); //To Scroll Down the webpage js.executeScript("window.scrollBy(0,50)"); //to Scroll the webpage to bottom of page js.executeScript("window.scrollBy(0,document.body.scrollHeight)"); } }
Hope this example helps little bit to understand how to use JavaScript in selenium – java code.