【问题标题】:NoSuchElementException after switching frames切换帧后出现 NoSuchElementException
【发布时间】:2013-08-20 13:21:27
【问题描述】:

更新

我想我看到了错误,我再次配置我的 Selenium IDE 并重新创建测试,当我在 Eclipse 中打开时,我在代码中看到了这个 cmets:

public void testEcsf3() throws Exception {
    driver.get(baseUrl + "/something.com");
    WebElement frame = driver.findElement(By.name("body"));
    driver.switchTo().frame(frame);
    //...
    //code for navigate to the target page
    //...
    // ERROR: Caught exception [ERROR: Unsupported command [selectWindow | name=body | ]]
    //Target page - another frame with name 'body'
    driver.findElement(By.xpath("//tr[28]/td[2]/a/font")).click();// <-- target element in target page
   //...
    //code for navigate to the target page
    //...
  }

问题是页面之间的流程有更多的名称为“body”的框架(我无法更改),我怎样才能做到这一点? 谢谢。

--

我正在尝试在 Eclipse 中使用 JUnit 来使用 Selenium 测试用例(在浏览器中可以)。 当我尝试运行测试用例时,我收到此错误:

org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"name","selector":"user"}
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Driver info: org.openqa.selenium.firefox.FirefoxDriver

注意:错误中提到的链接没有内容! 这是错误点:

driver.get(baseUrl + "/something.com");
driver.findElement(By.name("user")).sendKeys("aaa"); //<--
driver.findElement(By.name("password")).sendKeys("xxx");
driver.findElement(By.name("button0")).click();

【问题讨论】:

  • 您使用的是什么版本的 Selenium?听起来很旧。
  • 我使用的是最新版本,2.35。

标签: java firefox selenium junit


【解决方案1】:

我认为您的问题如下: 这行:driver.get(baseUrl + "/something.com"); 说他去这个页面,第二行说他立即搜索元素(所以浏览器根本没有时间加载页面)

所以试试这个:

WebDriverWait wait; 
wait = new WebDriverWait(webdriver, 10);
try{
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("user")));
}catch(TimeoutException e){
verifyElementPresent(locator);
}

或:

for (int second = 0;; second++) {
    if (second >= 60)
        fail("timeout");
    try {
        if (isElementPresent(By.name("user"))) {
            break;
        }
    } catch (Exception e) {

    }
    Thread.sleep(1000);
    }

【讨论】:

  • 您好 jollyjoyce1995,感谢您的关注。我试试你的建议,但没有用。这是代码: public void testEcsf3() throws Exception { driver.get(baseUrl + "/something.com");等待=新的WebDriverWait(驱动程序,10);尝试{ wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("user"))); }catch (Exception e) { isElementPresent(By.name("user")); } ... } 注意:我使用的是 FirefoxDriver(由 Selenium IDE 代码生成)。
【解决方案2】:

您是否绑定到驱动程序?你可以试试这个:

Selenium selenium = new WebDriverBackedSelenium(driver,"http://example.com");
selenium.open("http://something.com");

和可选的

selenium.waitForPageToLoad();

使用WebDriverBackedSelenium 类似,您尝试做的其他事情也更简单

selenium.type(String field,String text);

您可以查看 that 以获得 javadoc 和更深入的解释

【讨论】:

  • 我使用的是 FireFox 驱动,在 Selenium IDE 私有 WebDriver 驱动的测试用例中生成; ... 驱动程序 = 新的 FirefoxDriver();我认为和这个司机呆在一起很有趣,只有当它是唯一的选项时才会改变(我错了吗?)。谢谢。
  • 好的,所以你可以用你的驱动程序作为参数构造 WebDriverBackedSelenium 对象
【解决方案3】:

我停止了这个项目几天,今天我解决了这个问题。 这是代码:

    for (String handle : driver.getWindowHandles()) {
        driver.switchTo().window(handle);
    }
    WebElement body = driver.findElement(By.name("body"));
    driver.switchTo().frame(body);

我希望这对某人有所帮助。 谢谢。

【讨论】: