Sometimes you need to run Javascript native functions on your Web Elements that were identified through selenium, so, here is the scenario:
You have a web element that was initialized either through @findby annotation, or through page factory. You need to run some javascript code against this web element, like, for instance, the case when you need to scroll to an invisible element. By default, javascript doesn't provide a mean to access elements in the DOM through the xpath, which is widely used to identify web elements.
Fortunately, selenium web driver provides the mean to interact with the web element and here is the hack.
You have a web element that was initialized either through @findby annotation, or through page factory. You need to run some javascript code against this web element, like, for instance, the case when you need to scroll to an invisible element. By default, javascript doesn't provide a mean to access elements in the DOM through the xpath, which is widely used to identify web elements.
Fortunately, selenium web driver provides the mean to interact with the web element and here is the hack.
- Create a Javascript executer.
- WebDriver driver = new ChromeDriver();
- JavascriptExecutor jsExecuter; = (JavascriptExecutor) driver;
- Execute this java script command against your web driver.
- WebElement myWebElement; myWebElement = driver.findElement(By.xpath("//input[@type='file']"));
js.executeScript("argument[0].scrollToElement();",myWebElement
);
So, the hack is, argument[0] is replaced by the first parameter in the list at run time, which gives you the flexibility to define the element once, and don't bother yourself with the way it will be handled by javascript code.
- //Do the required actions in the tabs
Comments
Post a Comment