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. 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 ("...
In this blog I addresses most of non ordinary challenges that most of the automation testers face on daily basis.