org.openqa.selenium.NoSuchElementException - Selenium Test

时间:2016-06-12 20:13:11

标签: java selenium

在Java中,我编写了一个用于测试Yahoo Movies的Selenium测试。我正在从雅虎网站测试This Movie。但是我编写的代码(如下所示)抛出异常(如下所示)。我是Selenium的新手,所以请解决问题。

代码:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;



public class Tests {

    WebDriver driver;
    Wait<WebDriver> wait;
    boolean result;

    Tests() {
        driver = new FirefoxDriver();
        wait = new WebDriverWait(driver, 30);
        driver.get("http://www.yahoo.com/");
    }

    public static void main(String arg[]) {
        boolean result = new Tests().movies();
        System.out.println(result?"PASSED":"FAILED");
    }

    public boolean movies() {
        try {
            System.out.print("Testing Movies... ");
            driver.findElement(By.linkText("Movies")).click();
            driver.findElement(By.linkText("Finding Dory")).click();
            wait.until(new ExpectedCondition<Boolean>() {
                public Boolean apply(WebDriver webDriver) {
                    return webDriver.findElement(By.className("yvp-flash-video")) != null;
                }
            });
            return driver.findElement(By.id("Main")).getText().contains("'Finding Dory' Trailer");
        }
        catch(Exception exp) {
            exp.printStackTrace();
            return false;
        }
    }
}

例外:

Testing Movies... org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"link text","selector":"Finding Dory"}
Command duration or timeout: 5.08 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 16:57:40'
System info: host: 'Jahanzeb', ip: '10.99.14.207', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.7.0_79'
*** Element info: {Using=link text, value=Finding Dory}
Session ID: 5f14f1fa-85e4-471e-982f-27317dd766b7
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=WINDOWS, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=46.0.1}]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByLinkText(RemoteWebDriver.java:428)
    at org.openqa.selenium.By$ByLinkText.findElement(By.java:246)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355)
    at Tests.movies(Tests.java:189)
    at Main.main(Main.java:14)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"link text","selector":"Finding Dory"}
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 16:57:40'
System info: host: 'Jahanzeb', ip: '10.99.14.207', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.7.0_79'
Driver info: driver.version: unknown
    at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous903980554428331931webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10770)
    at <anonymous class>.FirefoxDriver.prototype.findElement(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous903980554428331931webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10779)
    at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous903980554428331931webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12661)
    at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous903980554428331931webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12666)
    at <anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous903980554428331931webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12608)
FAILED

2 个答案:

答案 0 :(得分:1)

我想你正试图点击一个&#34;链接&#34;命名为#34;寻找多莉&#34;在本页面: enter image description here

这不是链接 - 尝试手动点击它,根本无法点击它。
如果你点击它,没有任何反应,它只是不可解决的 - 这只是一个简单的文字&#34;找到Dory&#34;。

你可以做的是点击一个名为&#34;播放预告片&#34;的链接。位于文本下方的#34;查找Dory&#34;。

问题是有许多相同名称的链接&#34;播放预告片&#34;在此页面上,WebDriver不知道应该单击哪个链接,如果您尝试使用简单方法findElement( By.linkText('Play trailer')).click();,则会返回错误 您需要告诉WebDriver它应该点击位于文本&#34;查找Dory&#34;下的链接。要做到这一点,您需要比simle方法By.linkText更高级的定位策略。最受欢迎的两个是使用xpath或css表达式定位。

首先,您需要检查此页面的结构。在浏览器中打开此页面,然后按F12键 - 这将打开“开发人员工具”窗口(如何使用此工具超出此答案)。你会看到这样的事情: enter image description here

那是:

  • DIV标记包含文字&#34;查找Dory&#34;
  • 后跟A标记,其中包含文字&#34; Play预告片&#34;

您可以为上面的内容构建一个xpath表达式,找到一个链接&#34; Play预告片&#34;位于DIV标记下,其中包含&#34;查找Dory&#34;这样:

//div[contains(.,'Finding Dory')]/following-sibling::a[contains(.,'Play trailer')]

然后替换代码的这一行:

driver.findElement(By.linkText("Finding Dory")).click();

这一个:

driver.findElement(
   By.xpath(
     "//div[contains(.,'Finding Dory')]/following-sibling::a[contains(.,'Play trailer')]"
)).click();

答案 1 :(得分:0)

您可以尝试以下代码

driver.findElement(By.xpath(.// a [contains(text(),'Finding Dory'))]。click();

注意:如果“Finding Dory”有任何链接,那么只有上述代码可以使用。