Skip to main content
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("argument[0].scrollToElement();",myWebElement);
  • //Do the required actions in the tabs
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.

Comments

Popular posts from this blog

How to generate data using Faker Library and Beanshell in JMeter

Many times you face the situation when you need the flexibility to generate data in JMeter, sometimes the data might be complex, as in the case when you want to generate phone numbers in specific format, or ID numbers in specific format. To use the power of Java along with the simplicity of JMeter, fortunately, JMeter has the wonderful feature of Beanshell where you can call Java libraries. In the below example i will demonstrate how you can do that. Download the jar from https://mvnrepository.com/artifact/com.github.javafaker/javafaker Add the jar in your JMeter lib folder. Download the YML library from https://mvnrepository.com/artifact/org.yaml/snakeyaml Add the jar in your JMeter lib folder. Restart JMeter Add a Beanshell Sampler. Write the follwoing code: //In the code below I am getting a random Phone Number  import com.github.javafaker.Faker; Faker fakeData = new Faker(); String phoneNo = fakeData.phoneNumbe...
Second video in the advanced JMeter topics, Many of us use JMeter for performance testing, while the tool offers plenty of wonderful resources and plugins, a point of order is required when the project grows large, you need a framework to organize things and to ease the maintenance mission, check this youtube video https://www.youtube.com/watch?v=8kLWRdjnopw This project describes how to create a reusable JMeter script that takes into consideration the following aspects: 1- Re-usability 2- Maintainability 3- Extension We have used the following components to achieve our target: 1- Simple controller 2- Transaction controller 3- Module controller 4- Test fragment You can find the code on Github https://github.com/agnasser56/JMeterA...
GEAR UP FOLKS, Selenium 4 is here!!! What's new in Selenium 4: Selenium 4 is fully W3C compliant: This means, in simple terms, that there is no need to install drivers for different types of browsers anymore. To understand how this works, lets go back a little bit in time to the days of Selenium RC where the interaction with web browsers was done through injecting Javascript functions in the browser and use the injection javascript functions to drive the AUT.  What happened afterwards starting from Selenium 2 was a bit different. Each browser provided its own APIs that supports the automation. Hence, you will find Chrome browser, Geko driver for firefox and similar drivers for other types of browsers. The new thing in Selenium 4 is that it is fully compliant with W3C which means that no need to install any external web drivers, CHEERS!! Selenium IDE is back.  After deprecating Selenium IDE somewhere back in August 2018, it is coming again in Selenium 4 with the en...