2015-04-29 201 views
1

我使用硒webdriver單擊鏈接,但它會拋出NoSuchElementException。我使用的發現,當我執行瀏覽器搜索在檢查元素選項卡中選擇所需的鏈接的XPath,還當我在瀏覽器中執行下面的javascript - 控制檯,鏈接被點擊擺好,硒沒有點擊一個鏈接,而在JavaScript上,鏈接完美點擊

Javascript代碼:

var element = window.top.document.evaluate("//a[@id='clients']/i[@class='icon-chevron-down']" ,document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;if (element != null) { element.click(); } else { alert('Not Found'); } 

但是不知道爲什麼硒拋出下面的異常,我已經嘗試各種明確的等待,然後點擊。

org.openqa.selenium.NoSuchElementException: no such element 
    (Session info: chrome=42.0.2311.135) 
    (Driver info: chromedriver=2.9.248307,platform=Mac OS X 10.9.5 x86_64) (WARNING: The server did not provide any stacktrace information) 
Command duration or timeout: 45.05 seconds 
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html 
Build info: version: 'unknown', revision: 'unknown', time: 'unknown' 
System info: host: 'VDO105.local', ip: '172.16.0.70', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.9.5', java.version: '1.7.0_75' 
Session ID: b9f85e7103119d4c7b7ff265d02187ca 
Driver info: org.openqa.selenium.chrome.ChromeDriver 
Capabilities [{platform=MAC, acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, chrome={userDataDir=/var/folders/1v/pyj3pk396pb0z71_tlqcppgh0000gn/T/.org.chromium.Chromium.TePZ9p}, rotatable=false, locationContextEnabled=true, version=42.0.2311.135, takesHeapSnapshot=true, cssSelectorsEnabled=true, databaseEnabled=false, handlesAlerts=true, browserConnectionEnabled=false, webStorageEnabled=true, nativeEvents=true, applicationCacheEnabled=false, takesScreenshot=true}] 

這裏是鏈接的html代碼:

<a ng-show="sideBar.length" class="dropdown-toggle toggle-active" id="clients" href="javascript:void(0);"> 
<i class="icon-group"></i> 
<span class="menu-lv-1 ng-binding">Clients</span> 
<i class="icon-chevron-down"></i> 
</a> 

回答

0

當你網頁的JavaScript的填充這經常發生。當一個新的頁面出現後硒收到完成加載後。如果事後有所改變,硒將不會有這些變化。

聽起來這是你的問題。

爲此我創建了一些特殊的函數來代替selenium提供的特殊功能。這個類包含WebDriver實例。

public void click(final By by){ 
    act(by, 3, new Callable<Boolean>() { 
    public Boolean call() { 
     wait.until(ExpectedConditions.refreshed(ExpectedConditions.visibilityOfElementLocated(by))); 
     driver.findElement(by).click(); 
     logger.info("Button "+stripBy(by) + " clicked"); 
     return Boolean.TRUE; // FOUND IT 
    } 
    });} 

函數act是一個特殊的函數,用於重試元素上的speficyfied動作。

private void act(By by, int tryLimit, boolean mode, Callable<Boolean> method){ 
    logger.trace("Looking for element: " + stripBy(by)); 
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); 
    boolean unfound = true; 
    int tries = 0; 
    while (unfound && tries < tryLimit) { 
    tries += 1; 
    try { 

     WebDriverWait wait = new WebDriverWait(driver, 500); 
     wait.until(ExpectedConditions.refreshed(ExpectedConditions.visibilityOfElementLocated(by))); 
     wait.until(ExpectedConditions.refreshed(ExpectedConditions.elementToBeClickable(by))); 
     unfound = !method.call(); // FOUND IT, this is negated since it feel more intuitive if the call method returns true for success 
     } catch (StaleElementReferenceException ser) { 
      logger.error("ERROR: Stale element exception. " + stripBy(by)); 
      unfound = true; 
     } catch (NoSuchElementException nse) { 
      logger.error("ERROR: No such element exception. " + stripBy(by)+"\nError: "+nse); 
      unfound = true; 
     } catch (Exception e) { 
      logger.error(e.getMessage()); 
     } 
    } 

    driver.manage().timeouts().implicitlyWait(Constants.DEFAULT_IMPLICIT_WAIT, TimeUnit.SECONDS); 

    if(unfound) 
     Assert.assertTrue(false,"Failed to locate element by locator " + stripBy(by));}