selenium test抛出org.openqa.selenium.NoSuchElementException

时间:2016-06-12 08:16:26

标签: java selenium

我为雅虎网站写了一个selenium测试。在这个测试中,我正在测试Yahoo News。但是这段代码抛出了异常。那么请解决问题? 我正在使用Eclipse IDE和Firefox作为浏览器。

代码:

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[]) {
        new Tests().news();
    }

    public boolean news() {
        try {
            System.out.print("Testing News... ");
            driver.findElement(By.linkText("https://www.yahoo.com/news/")).click();
            driver.findElement(By.partialLinkText("/news/world/")).click();
            wait.until(new ExpectedCondition<Boolean>() {
                public Boolean apply(WebDriver webDriver) {
                    return webDriver.findElement(By.id("th-title")) != null;
                }
            });
            return driver.findElement(By.id("th-title")).getText().contains("World");
        }
        catch(Exception exp) {
            exp.printStackTrace();
            return false;
        }
    }
}

例外:

Testing News... org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"link text","selector":"https://www.yahoo.com/news/"}
Command duration or timeout: 21.55 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=https://www.yahoo.com/news/}
Session ID: 7576b452-dcb2-448f-844c-6c8b499561f1
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}]
FAILED
    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.news(Tests.java:72)
    at Main.main(Main.java:14)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"link text","selector":"https://www.yahoo.com/news/"}
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/anonymous2826618494991255784webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10770)
    at <anonymous class>.FirefoxDriver.prototype.findElement(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous2826618494991255784webdriver-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/anonymous2826618494991255784webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12661)
    at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous2826618494991255784webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12666)
    at <anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous2826618494991255784webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12608)

1 个答案:

答案 0 :(得分:4)

请检查Yahoo page
请按Ctrl-F并尝试查找文字:“https://www.yahoo.com/news/”&gt;
那里没有这样的文字。

但是如果你查看这个页面的html源代码,你会发现这个带有href的A标签attriibutte =“”https://www.yahoo.com/news/“:

<a class="C(#fff) Td(n) Td(u):h" href="https://www.yahoo.com/news/" 
 data-reactid=".pgwo63s1xy.$tgtm-UH-0-Header.1.0.0:$news.0">News</a>

但此链接的“链接文字”不是“https://www.yahoo.com/news/”,而是“新闻”
By#linkText方法正在寻找链接文字(在这种情况下“新闻”),而不是href attributte。

您需要替换此命令:

 driver.findElement(By.linkText("https://www.yahoo.com/news/")).click(); 

这一个:

driver.findElement(By.linkText("News")).click();