2016-06-12 150 views
1

在Java中,我編寫了用於測試Yahoo電影的Selenium測試。我正在從雅虎網站測試This Movie。但是我寫的代碼(下面給出)會拋出一個Exception(下面給出)。我是Selenium的新手,所以請解決問題。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[]) { 
     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/[email protected]/components/driver-component.js:10770) 
    at <anonymous class>.FirefoxDriver.prototype.findElement(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous903980554428331931webdriver-profile/extensions/[email protected]/components/driver-component.js:10779) 
    at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous903980554428331931webdriver-profile/extensions/[email protected]/components/command-processor.js:12661) 
    at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous903980554428331931webdriver-profile/extensions/[email protected]/components/command-processor.js:12666) 
    at <anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous903980554428331931webdriver-profile/extensions/[email protected]/components/command-processor.js:12608) 
FAILED 

回答

1

我assumming您嘗試點擊 「鏈接」 命名爲 「海底總動員2」 在此頁上: enter image description here

但是thi s不是鏈接 - 嘗試手動點擊它,它根本無法點擊它。
如果你點擊它,什麼也沒有發生,它只是不可能
- 這不過是一個簡單的文字「尋找D魚」。

您可以做的是點擊位於文本「Finding Dory」下面的名爲「Play trailer」的鏈接。

的問題是,有相同名稱此頁面上的「播放預告片」,並webdriver的不知道應該點擊哪個鏈接環節多,如果你嘗試一個簡單的方法findElement(By.linkText('Play trailer')).click();

你需要返回一個錯誤告訴WebDriver它應該點擊文本「Finishing Dory」下的鏈接。要做到這一點,您需要比虛擬方法By.linkText更先進的定位策略。最流行的兩種是使用xpath或css表達式進行定位。

首先您需要檢查此頁面的結構。在瀏覽器中打開此頁面,然後按F12鍵 - 這將打開「開發人員工具」窗口(如何使用此工具超出此答案)。你會看到這樣的事情: enter image description here


即:包含文本「海底總動員2」

  • 後面包含文本「播放預告片」 A標籤

    • DIV標籤

    您可以爲上述內容創建一個xpath表達式,該表達式找到位於0下的鏈接「播放預告片」標籤包含以這種方式 「海底總動員2」:

    //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(); 
    
  • 0

    你可以試試以下代碼

    driver.findElement(By.xpath(.// a [contains(text(),'Fin ding Dory'))]。點擊();

    注意:如果「Finishing Dory」有任何鏈接,那麼只有上面的代碼纔有效。