2

我在 Windows XP 上使用最新的 Selenium 代码,2.25 IEDriver 2.25.2 32 位,IE 8 32 位。使用下面的简单代码清单,我永远不会找到“找到的元素!!” 如果我使用 InternetExplorerDriver。它打开页面,读取页面标题,但 findElement 调用失败。如果我换成 FirefoxDriver 就可以了。我已经能够通过集线器让 IE 9 64 位在远程 PC 上工作(取消注释前两行并注释掉两个 WebDriver 行)。

不明白为什么 IE 32 位会失败。

============================================

package com.company.test;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class App 
{
    public static void main( String[] args ) throws InterruptedException, MalformedURLException
    {
        // Grid driver
//        DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
//        WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);

        // Local driver
//        WebDriver driver = new FirefoxDriver();

        WebDriver driver = new InternetExplorerDriver();
        try {
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
            driver.get("http://seleniumhq.org/");
            String pageTitle = driver.getTitle();
            System.out.println("pageTitle=" + pageTitle);
            driver.findElement(By.id("q")).clear();
            System.out.println("Found element!!");
            driver.findElement(By.id("q")).sendKeys("test");
            driver.findElement(By.id("submit")).click();
        } finally {
            driver.quit();
        }
        System.out.println("Done");
    }
}

==================================================== ==========

我能够让 IEDriver 生成 TRACE 日志这是感兴趣的片段:

T 2012-08-09 16:04:09:516 IECommandExecutor.cpp(604) Entering IECommandExecutor::LocateElement
T 2012-08-09 16:04:09:516 IECommandExecutor.cpp(587) Entering IECommandExecutor::GetElementFindMethod
T 2012-08-09 16:04:09:516 ElementFinder.cpp(33) Entering ElmentFinder::FindElement
T 2012-08-09 16:04:09:516 IECommandExecutor.cpp(439) Entering IECommandExecutor::GetCurrentBrowser
T 2012-08-09 16:04:09:516 IECommandExecutor.cpp(445) Entering IECommandExecutor::GetManagedBrowser
T 2012-08-09 16:04:09:516 ElementFinder.cpp(468) Entering ElementFinder::SanitizeCriteria
T 2012-08-09 16:04:09:516 ElementFinder.cpp(479) Entering ElementFinder::ReplaceAllSubstrings
T 2012-08-09 16:04:09:516 ElementFinder.cpp(479) Entering ElementFinder::ReplaceAllSubstrings
T 2012-08-09 16:04:09:516 Browser.cpp(91) Entering Browser::GetDocument
I 2012-08-09 16:04:09:516 Browser.cpp(95) No child frame focus. Focus is on top-level frame
T 2012-08-09 16:04:09:516 IECommandExecutor.cpp(187) Entering IECommandExecutor::OnGetResponseLength
T 2012-08-09 16:04:09:532 IECommandExecutor.cpp(187) Entering IECommandExecutor::OnGetResponseLength
T 2012-08-09 16:04:09:532 Browser.cpp(451) Entering Browser::GetDocumentFromWindow
T 2012-08-09 16:04:09:532 Script.cpp(40) Entering Script::Initialize
T 2012-08-09 16:04:09:532 Script.cpp(210) Entering Script::Execute
T 2012-08-09 16:04:09:532 Script.cpp(577) Entering Script::CreateAnonymousFunction
T 2012-08-09 16:04:09:547 IECommandExecutor.cpp(187) Entering IECommandExecutor::OnGetResponseLength
W 2012-08-09 16:04:09:547 Script.cpp(594) -2147024891 [Access is denied.]: Unable to execute code, call to IHTMLWindow2::execScript failed
W 2012-08-09 16:04:09:547 Script.cpp(221) Cannot create anonymous function
W 2012-08-09 16:04:09:547 ElementFinder.cpp(86) Unable to create criteria object for mechanism 00FBA030 and criteria00FB9FB0

我相信问题出在上面红色突出显示的部分,但我不知道如何解决这个问题。有人有想法么?

4

1 回答 1

1

检查您的浏览器安全设置以确保启用了活动脚本。要在 IE9 中启用,请执行以下操作: 1. 单击“工具”图标。(工具图标在 IE9 的右上角,或者工具也可以在菜单栏上作为“工具”找到)。2. 然后点击 -> Internet 选项” -> 安全选项卡 -> 自定义级别按钮 -> 滚动到靠近底部的“脚本”部分,然后在“活动脚本”下选择启用。 3. 点击确定。

于 2012-12-19T21:32:48.007 回答