JavaScript Executor using Selenium WebDriver

0 votes

I am using JavaScript for my code.

I used an object of JavaScriptExecutor, but executeScript() method is not present. It shows error when I use executeScript() method.

Below is my code:

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.JavascriptExecutor;

public class NewDemo{

    public static void main(String[] args) {

        WebDriver driver=new FirefoxDriver();

        driver.get("http://google.com");

        driver.manage().window().maximize();

        System.out.println(driver.getCurrentUrl());

        JavaScriptExecutor js=(JavaScriptExecutor) driver;

        String domain_name=(String) js.executeScript("return document.domain");

                System.out.println(doamin_name);

    }

}
Apr 9, 2018 in Selenium by xyz
• 1,550 points
178 views

1 answer to this question.

0 votes

Its working fine but the problem in your code is with JavaScriptExecutor, i.e. s should be in lower case

See this or us this:

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;

public class NewDemo {

public static void main(String[] args) {

    WebDriver driver=new FirefoxDriver();

    driver.get("http://google.com ");

    driver.manage().window().maximize();

answered Apr 9, 2018 by Vardy
• 2,280 points

Related Questions In Selenium

0 votes
1 answer

Capturing JavaScript errors with Selenium WebDriver using Java

There is logs Beta version in WebDriver driver.manage().logs().get(LogType.BROWSER); Console output will be ...READ MORE

answered May 18, 2018 in Selenium by Samarpit
• 5,130 points
171 views
0 votes
1 answer
0 votes
1 answer

I tried to open a new website when a new tab is opened using Selenium Webdriver

Try this code: ArrayList<String> tabs = new ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,050 points
705 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,050 points
276 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
639 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
265 views
0 votes
1 answer

Select an item from a dropdown list using Selenium WebDriver

Use this then it will work - new ...READ MORE

answered Apr 9, 2018 in Selenium by Vardy
• 2,280 points
1,456 views
0 votes
1 answer

Using gettext() method for the specific element using Selenium WebDriver

Mistake is that u r printing the ...READ MORE

answered Apr 10, 2018 in Selenium by Vardy
• 2,280 points
3,946 views