Skip to main content

Posts

Showing posts from September, 2018
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 ("...
Today I was faced by an odd behavior that is: when opening the page in chrome it was displayed in a larger resolution than the normal one which led to some tabs in the page being invisible. I figured out a solution to this weird issue by doing a java script hack as follows: Create a Javascript executer. WebDriver driver = new ChromeDriver (); JavascriptExecutor jsExecuter; = ( JavascriptExecutor )  driver ;     Execute this java script command against your web driver  js . executeScript ( " document.body.style.zoom='90%'" ); //Do the required actions in the tabs After all is done, don't forget to reset the browser to its default settings. js . executeScript ( " document.body.style.zoom='100%'" );