UrbanPro
true

Learn Selenium from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Use JavascriptExecutor to do many cool things in Selenium

Srinivas K
23/11/2018 0 0

Javascript is a compelling language to interact with DOM. So it would make sense to use Javascript to run in the browser using Selenium whenever there is no other way to work on a particular element using Selenium.

JavascriptExecutor is an interface.
By creating object JavascriptExecutor jse=(JavascriptExecutor)driver;
we can use method executeScript to execute anything in the browser using javascript.

Firstly, some of the basic things like a scroll, get URL, Title can be performed using javascript with the following code.

window.scrollBy(0,400); // x, y --> x is for horizontal scroll. y is for vertical scroll. And if you pass negative value it scrolls in other direction.

document.URL; // get URL of current browser
document.domain; // get domain of browser
document.title; // get title of the browser page.

Now, this code we can execute in selenium using.

JavascriptExecutor jse=(JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,400);");

it would scroll down by 400 pixels from the current position.

You could also javascript returning some data such as in boolean, long, String, WebElement and using in your selenium code.

Eg:
String currentURL= jse.executeScript(""return document.URL").toString();

Similarly, you can write javascript to work on any element for that matter and call it in Selenium.

Another approach you can use with JavascriptExecutor is to use elements created to execute in Javascript.

Eg:

WebElement button=driver.findElement(By.name("Insert"));
button.click();
jse.executeScript("arguments[0].click();",button);

0 Dislike
Follow 2

Please Enter a comment

Submit

Other Lessons for You

What is Selenium?
In simple words, Selenium automates browsers. It is a robust set of tools that supports rapid development of test automation for web-based applications. Cool Features: Works on every system,...

doWhile example in Java
public class doWhilePracticleEx { public void test() { } public static void main(String args) { String q1 = "Who is PM of India?"; String a1 = "Sonia Gandhi"; String a2 = "Rahul Gandhi"; String a3...
S

Sarthak C.

0 0
0

Selenium waits
In Selenium web driver, the biggest challenge is to synchronize your script with the Web application under test. To make this synchronization work, we have concept of waits. Although we can use Thread.sleep()...

How to refresh a page without using contextclick?
1. Using sendKeys methoddriver.findElement(By.id("firstname-placeholder")).sendKeys(Keys.F5);2. Using navigate.refresh() methoddriver.navigate().refresh();3. Using navigate.to() methoddriver.navigate().to(driver.getCurrentUrl());4....

How to Read Data From Property File
1. Create a property file using New > File > Given name - like config.properties (it must be saved with .properties). 2. Add data in config.properties like: browser = chrome environment = qa 3....
S

Sarthak C.

0 0
0
X

Looking for Selenium Classes?

The best tutors for Selenium Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Selenium with the Best Tutors

The best Tutors for Selenium Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more