By Mohammad Waseem, Community Contributor - February 7, 2023
Let’s take a look at various components.
- executeScript method – This method executes the test script in the context of the currently selected window or frame. The script in the method runs as an anonymous function. If the script has a return statement, the following values are returned:
- For an HTML element, the method returns a WebElement.
- For a decimal, the method returns Long.
- For a non-decimal number, the method returns Long.
- For a Boolean, the method returns Boolean.
- For other cases, the method returns a String.
- Import the package
- Create a reference
[java] js.executeScript(“document.getElementByID(‘element id ’).click();”); [/java]
[java] js.executeScript(“document.getElementByID(‘element id ’).value = ‘xyz’;”); [/java]
[java] js.executeScript(“document.getElementByID(‘element id ’).checked=false;”); [/java]
[java] js.executeScript(“location.reload()”); [/java]
The above code snippets show the syntax to perform specific operations.
Use Case Scenarios
Let’s look at another example, which uses the executeAsyncScript method to scroll down to the bottom of a webpage.