无法在 Selenium Webdriver (JAVA) 中启动 Internet Explorer 或 Chrome
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19386594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to start Internet Explorer or Chrome in Selenium Webdriver (JAVA)
提问by SteveA
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.
我正在尝试使用 Webdriver 启动一个 IE 实例。我不明白为什么我会收到这些错误,我的代码似乎与我在网上能找到的每个例子都一样。
我正在使用 Java 和 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.
显示以下错误,所有这些错误都在“System.setProperty”行上。
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
此行上的多个标记 - 标记“"webdriver.ie.driver"" 上的语法错误,无效的 FormalParameterList - 标记上的语法错误,错位的构造 - 标记上的语法错误,应为 FormalParameter
Please note that I have the exact same problem if I try to use Chrome with this code:
请注意,如果我尝试将 Chrome 与以下代码一起使用,我会遇到完全相同的问题:
File file = new File("C:/selenium/chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
WebDriver driver = new ChromeDriver();
采纳答案by JacekM
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();
}
}
回答by Shessuky
try this :
尝试这个 :
I'm using "mvn test" to lunch the test process so the path of the IE driver may be changed
我正在使用“mvn test”来完成测试过程,因此可能会更改 IE 驱动程序的路径
File file = new File("classes/tools/IEDriverServer.exe");
Use IE driver with Capabilities
使用具有 Capabilities 的 IE 驱动程序
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 :)
它可能会帮助你:)
回答by Jolin
Actually, on the updated eclipse version, you might have to use @suppressWarnings
实际上,在更新的 eclipse 版本上,您可能必须使用 @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();
}
}
回答by Sathish D
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();
}
}
回答by Amit Kumar
Do the below process.
执行以下过程。
import org.openqa.selenium.ie.InternetExplorerDriver;
导入 org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
导入 org.openqa.selenium.remote.DesiredCapabilities;
if (browserName.equalsIgnoreCase("InternetExplorer")) {
if (browserName.equalsIgnoreCase("InternetExplorer")) {
DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
System.setProperty("webdriver.ie.driver", "drivers/IEDriverServer.exe"); caps.setCapability( InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
System.setProperty("webdriver.ie.driver", "drivers/IEDriverServer.exe"); caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,真);
caps.setCapability("nativeEvents", false); browser = new InternetExplorerDriver(caps);
caps.setCapability("nativeEvents", false); 浏览器 = 新 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.
然后,在 IE 中,从工具菜单(或更高版本工具栏中的齿轮图标)中,选择“Internet 选项”。转到安全选项卡。在每个区域的对话框底部,您应该会看到一个标记为“启用保护模式”的复选框。对于每个区域,将复选框的值设置为相同的值(选中或未选中)。
I have applied the same thing at my end, it works fine.
我最后应用了同样的东西,它工作正常。