org.openqa.selenium.NoSuchElementException - Selenium Test [英] org.openqa.selenium.NoSuchElementException - Selenium Test

查看:219
本文介绍了org.openqa.selenium.NoSuchElementException - Selenium Test的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,我编写了一个用于测试Yahoo Movies的Selenium测试。我正在从雅虎网站上测试



这不是链接 - 尝试手动点击它,根本无法点击它。

如果你点击它,没有任何反应,它只是不可解决的问题 - 这只是一个简单的文字寻找多莉。


你能做什么是单击文本Finding Dory下方名为Play trailer的链接。

问题是这个页面上有许多同名播放预告片的链接,而且WebDriver不知道应该点击哪个链接,如果你尝试一个简单的方法会返回错误 findElement(By.linkText('Play trailer'))。click();


你需要告诉WebDriver它应该点击链接位于寻找多莉字样下。要做到这一点,你需要比simle方法 By.linkText 更高级的定位策略。最受欢迎的两个是使用xpath或css表达式进行定位。


首先,您需要检查此页面的结构。在浏览器中打开此页面,然后按F12键 - 这将打开开发人员工具窗口(如何使用此工具超出此答案)。您将看到如下内容:






那个是:




  • DIV 包含文字Finding Dory的标签

  • 后跟 A 标签,其中包含文字Play预告片



你可以为上面的内容构建一个xpath表达式,找到一个位于 DIV 标签下的链接Play预告片,其中包含查找Dory方式:

  // div [包含(。,'查找Dory')] / follow-sibling :: a [包含(。,'播放预告')] 

然后替换代码的这一行:

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

这一个:

  driver.findElement(
By.xpath(
//div [contains(.,'Finding Dory')] / following-sibling :: a [contains(。, '播放预告')]
))。click();


In Java, I have written a Selenium test for testing Yahoo Movies. I am testing This Movie from yahoo website. But the code that I have written (given below) is throws an Exception (given below). I am new to Selenium, so please solve the problem.

Code:

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;
        }
    }
}

Exception:

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

解决方案

I am assumming that you are trying to click a "link" named "Finding Dory" on this page:

But this is not a link - try to click it manually, it simply is not possible to click on it.
If you click on it, nothing happens, it's just not clickeable
- this is nothing but a simple text "Finding Dory".

What you can do is to click a link named "Play trailer" that is located below the text "Finding Dory".

The problem is that there are many links with the same name "Play trailer" on this page, and WebDriver doesn't know which link should be clicked, and returns an error if you try a simple method findElement( By.linkText('Play trailer')).click();

You need to tell WebDriver that it should click on the link that is located under the text "Finding Dory". To do it you need more advanced locating strategies than a simle method By.linkText. The two the most popular are locating using xpath or css expressions.

First you need to examine a structure of this page. Open this page in the browser, and press F12 key - this opens Developer Tools window (how to use this tool is beyond this answer). You will see something like this:


That is:

  • DIV tag that contains a text "Finding Dory"
  • followed by A tag that contains a text "Play trailer"

You can build an xpath expression for the above, that finds a link "Play trailer" located under a DIV tag that contains "Finding Dory" in this way :

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

and then replace this line of your code:

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

with this one:

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

这篇关于org.openqa.selenium.NoSuchElementException - Selenium Test的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆