2016-06-12 81 views
-2

我寫了一個雅虎網站的硒測試。在這個測試中,我正在測試Yahoo News。但是這段代碼拋出了一個異常。那麼請解決問題? 我使用Eclipse IDE和Firefox作爲瀏覽器。硒測試拋出org.openqa.selenium.NoSuchElementException

代碼:

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/[email protected]/components/driver-component.js:10770) 
    at <anonymous class>.FirefoxDriver.prototype.findElement(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous2826618494991255784webdriver-profile/extensions/[email protected]/components/driver-component.js:10779) 
    at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous2826618494991255784webdriver-profile/extensions/[email protected]/components/command-processor.js:12661) 
    at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous2826618494991255784webdriver-profile/extensions/[email protected]/components/command-processor.js:12666) 
    at <anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous2826618494991255784webdriver-profile/extensions/[email protected]/components/command-processor.js:12608) 

回答

4

請檢查Yahoo page
請按Ctrl-F並嘗試查找文本:「https://www.yahoo.com/news/」>
這裏沒有這樣的文字。

但如果你看看這個網頁的HTML源代碼,
你會發現這與HREF 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/」,但是‘新聞’。
將通過#LINKTEXT方法尋找鏈接文本(在這種情況下,‘新聞報’),而不是HREF attributte

您需要更換這個命令:

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

這一個:

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

請回答這個問題https://stackoverflow.com/questions/37778640/org-openqa-selenium-nosuchelementexception-selenium-test –