【问题标题】:Error when my selenium script is launching Internet Explorer我的 selenium 脚本启动 Internet Explorer 时出错
【发布时间】:2017-03-29 13:24:33
【问题描述】:

当我的测试脚本尝试打开 Internet Explorer 时,出现错误。

这是我的测试脚本:

package selenium;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.Select;

public class RegistrationTest{

public  int a = 1;

@Test //This is JUnit annotation

public void testRegister(){

        WebDriver driver ;
        System.setProperty("webdriver.ie.driver","IEDriverServer.exe");

        DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
        capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);

        driver = new InternetExplorerDriver(capabilities);

        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);

        driver.navigate().to("http://newtours.demoaut.com/");

        driver.findElement(By.linkText("REGISTER")).click();
        driver.findElement(By.name("firstName")).sendKeys("User1");
        driver.findElement(By.name("lastName")).sendKeys("Surname1");
        driver.findElement(By.name("phone")).sendKeys("123456789");
        driver.findElement(By.name("userName")).sendKeys("user1@test.com");
        driver.findElement(By.name("address1")).sendKeys("Test Address");
        driver.findElement(By.name("city")).sendKeys("Test City");
        Select select = new Select(driver.findElement(By.name("country")));
        select.selectByVisibleText("ANGOLA");
        driver.findElement(By.name("email")).sendKeys("user1@test.com");
        driver.findElement(By.name("password")).sendKeys("user1");
        driver.findElement(By.name("confirmPassword")).sendKeys("user1");
        driver.findElement(By.name("register")).click();

        driver.quit();

}           

 }

我的 POM.xml 是:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cgi.nouguierc</groupId>
<artifactId>WD-Automation</artifactId>
<version>1.0.0-SNAPSHOT</version>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.0.2</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>selenium.MainJar</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
               <execution>
                     <id>copy-dependencies</id>
                     <phase>prepare-package</phase>
                     <goals>
                        <goal>copy-dependencies</goal>
                     </goals>
                     <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                     </configuration>
               </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.3.1</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>
</dependencies>
</project>

当我启动 RegistrationTest 时,互联网正在打开,转到地址“http://newtours.demoaut.com/”,然后什么也不做。

在控制台中我有一些错误:

Started InternetExplorerDriver server (64-bit)
3.0.0.0
Listening on port 44920
Only local connections are allowed
mars 29, 2017 3:16:07 PM org.openqa.selenium.remote.ProtocolHandshake    createSession
INFOS: Detected dialect: OSS

关键是我无法选中或取消选中“启用受保护...”框(请参阅下面的屏幕截图),因为我在工作网络上,所以我无法做我想做的事。

Screenshot of the checkbox

也许有人可以帮助我:)

【问题讨论】:

  • 提供IE驱动的完整路径后可以试一试:String driverPath = "C:\\Utility\\BrowserDrivers\\";System.setProperty("webdriver.ie.driver", driverPath+"IEDriverServer.exe");
  • 现在有效了吗?
  • 它没有解决我的问题...抱歉
  • 和你遇到的问题一样吗?
  • 是的,完全一样。只有一行:Listening on port 41586 which is different

标签: maven selenium internet-explorer junit


【解决方案1】:

尝试提供 IEDriverServer.exe

的完整路径
System.setProperty("webdriver.ie.driver", System.getProperty("user.dir")+"\\IEDriverServer.exe");

您提到的控制台输出绝对没问题,这不是错误。如果您注意到,您会在 Chromefirefox

中看到类似的内容
Started InternetExplorerDriver server (32-bit)
3.3.0.0
Listening on port 17384
Only local connections are allowed
Mar 29, 2017 7:33:32 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS

您的实际问题是,在 Intenet Explorer 的情况下,Selenium 无法找到 REGISTER Webelement,这在 Chrome 和 Firefox 中都可以正常工作。

在 IE 7 或更高版本的 Windows Vista 或 Windows 7 上,您必须将每个区域的保护模式设置设置为相同的值。该值可以打开或关闭,只要每个区域都相同。要设置保护模式设置,请从“工具”菜单中选择“Internet 选项...”,然后单击“安全”选项卡。对于每个区域,标签底部都会有一个标记为“启用保护模式”的复选框。此外,对于 IE 10 及更高版本,必须禁用“增强保护模式”。此选项位于 Internet 选项对话框的高级选项卡中。 浏览器缩放级别必须设置为 100%,以便可以将本机鼠标事件设置为正确的坐标。 仅对于 IE 11,您需要在目标计算机上设置一个注册表项,以便驱动程序可以保持与它创建的 Internet Explorer 实例的连接。对于 32 位 Windows 安装,您必须在注册表编辑器中检查的项是 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE。对于 64 位 Windows 安装,密钥是 HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE。请注意,FEATURE_BFCACHE 子项可能存在也可能不存在,如果不存在,则应创建它。重要提示:在此键中,创建一个名为 iexplore.exe 且值为 0 的 DWORD 值。

我的猜测是您的问题与保护模式有关。

【讨论】:

  • 给出完整路径并没有解决我的问题。我在 Internet Explorer 上执行此操作,因为我无法在我客户的计算机上安装另一个。是的,我知道这有点烦人!
  • 那我该怎么办?因为 Internet Explorer 找到了网址,但没有按照我的指示进行操作。
  • 更新了我的解决方案。
  • 我认为你是对的。但这是一台客户端计算机。所以我无法更改该复选框。所以我无法解决我的问题
  • 我尝试了所有选项,仅通过更改 Internet Explorer 设置脚本运行良好如果您认为我提供了正确的解决方案,那么请接受该解决方案。
【解决方案2】:

这是您问题的解决方案-

用这个替换你的代码:

    DesiredCapabilities capabilities = new DesiredCapabilities().internetExplorer();
    capabilities.setCapability("platform", "WIN8"); 
    capabilities.setCapability("version", "11"); 
    capabilities.setCapability("browserName", "internet explorer");
    capabilities.setCapability("ignoreProtectedModeSettings",1);
    capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
    driver = new InternetExplorerDriver(capabilities);
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);

    driver.navigate().to("http://newtours.demoaut.com/");

    driver.findElement(By.linkText("REGISTER")).click();
    driver.findElement(By.name("firstName")).sendKeys("User1");
    driver.findElement(By.name("lastName")).sendKeys("Surname1");
    driver.findElement(By.name("phone")).sendKeys("123456789");
    driver.findElement(By.name("userName")).sendKeys("user1@test.com");
    driver.findElement(By.name("address1")).sendKeys("Test Address");
    driver.findElement(By.name("city")).sendKeys("Test City");
    Select select = new Select(driver.findElement(By.name("country")));
    select.selectByVisibleText("ANGOLA");
    driver.findElement(By.name("email")).sendKeys("user1@test.com");
    driver.findElement(By.name("password")).sendKeys("user1");
    driver.findElement(By.name("confirmPassword")).sendKeys("user1");
    driver.findElement(By.name("register")).click();

    driver.quit();

最后的努力是:在 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet 下查看注册表 设置\区域键。在那里,您应该看到 0-5 号键。在键 1-4 下,查找名为 2500 的值,并列出每个键的值,或者如果该值不存在。

话虽如此,我希望您通过 this link 在 Selenium 用户组中明确讨论该问题。

您也可能希望通过 this link 在 Selenium Developers Group 中讨论该问题。

如果对你有帮助,请告诉我。

【讨论】:

  • 你改变了什么?
  • 现在工作了吗?让我们先解决问题,然后我会解释。
  • 它不工作。我有同样的问题。 Internet Explorer 已打开,但没有对其进行任何操作。输出 Junit 是:org.openqa.selenium.NoSuchElementException: Unable to find element with link text == REGISTER (WARNING: The server did not provide any stacktrace information)
  • @Celia 太好了,我们已经克服了最初的 IE 问题。可以提供元素 id/name/xpath 而不是 linktext 吗?
  • 什么意思?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-18
  • 2023-04-10
  • 2020-04-16
  • 1970-01-01
  • 2015-12-22
  • 1970-01-01
相关资源
最近更新 更多