简体   繁体   中英

Unable to start Internet Explorer or Chrome in Selenium Webdriver (JAVA)

I am trying to start up an IE instance using Webdriver. I can't figure out why I'm receiving these errors, my code appears to be identical to every example I can find on the web.
I'm using Java and testng.

Here is the code:

import java.io.File;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.WebDriver;

public class Tests {

    File file = new File("C:\\selenium\\IEDriverServer.exe");
    System.setProperty("webdriver.ie.driver", file.getAbsolutePath() );  
    WebDriver driver = new InternetExplorerDriver();
}

The following errors are displaying, all of these errors are on the "System.setProperty" line.

Multiple markers at this line - Syntax error on token ""webdriver.ie.driver"", invalid FormalParameterList - Syntax error on token(s), misplaced construct(s) - Syntax error on tokens, FormalParameter expected instead

Please note that I have the exact same problem if I try to use Chrome with this code:

File file = new File("C:/selenium/chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
WebDriver driver = new ChromeDriver();

You are running your code from inside class instead of running it from inside method. Covert it to something like

import java.io.File;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.WebDriver;

public class Tests {
    public static void main(String[] args) { // <-- you need a method!
       File file = new File("C:\\selenium\\IEDriverServer.exe");
       System.setProperty("webdriver.ie.driver", file.getAbsolutePath() );  
       WebDriver driver = new InternetExplorerDriver();
    }
}

try this :

I'm using "mvn test" to lunch the test process so the path of the IE driver may be changed

File file = new File("classes/tools/IEDriverServer.exe");

Use IE driver with Capabilities

DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
caps.setCapability("ignoreZoomSetting", true);
caps.setCapability("nativeEvents", false);
WebDriver driver = new InternetExplorerDriver(caps);

It may help you :)

Actually, on the updated eclipse version, you might have to use @suppressWarnings

package Login;

import java.io.File;

import org.openqa.selenium.ie.InternetExplorerDriver;

import org.openqa.selenium.WebDriver;

public class Login {

    public static void main(String[] args) { 

       File file = new File("C:\\Users\\IEDRiverServer.exe");
       System.setProperty("webdriver.ie.driver", file.getAbsolutePath() );  

       @SuppressWarnings("unused")
       WebDriver driver = new InternetExplorerDriver();

       } 
}

Simple example:

public class IE {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

            System.setProperty("webdriver.ie.driver", "D:\\Sathish\\soft\\SELENIUM\\LatestDownloads\\selenium\\IEDriverServer.exe");
            WebDriver driver = new InternetExplorerDriver();
            driver.get("www.google.com");
            driver.findElement(By.id("gbqfq")).sendKeys("abc");
            driver.close();

    }

}

Do the below process.

import org.openqa.selenium.ie.InternetExplorerDriver;

import org.openqa.selenium.remote.DesiredCapabilities;

if (browserName.equalsIgnoreCase("InternetExplorer")) {

DesiredCapabilities caps = DesiredCapabilities.internetExplorer();

System.setProperty("webdriver.ie.driver", "drivers/IEDriverServer.exe"); caps.setCapability( InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);

caps.setCapability("nativeEvents", false); browser = new InternetExplorerDriver(caps);

Then after, In IE, from the Tools menu (or the gear icon in the toolbar in later versions), select "Internet options." Go to the Security tab. At the bottom of the dialog for each zone, you should see a check box labeled "Enable Protected Mode." Set the value of the check box to the same value, either checked or unchecked, for each zone.

I have applied the same thing at my end, it works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM