2014-02-11 32 views
0

我正在使用Selenium WebDriver並使用Java。如果我執行註銷功能,它不會通過ID找到該元素。下面是代碼:元素未找到,其他部分被執行 - Selenium web驅動程序

enter image description here

Log.info("Clicking on Logout button"); 
//driver.findElement(By.id("moreLink")).click(); 
if(existsElement("logoutLink") == true) { 
    WebElement menuHoverLink = driver.findElement(By.id("logoutLink")); 
    actions.moveToElement(menuHoverLink).click().perform(); 
    Thread.sleep(6000); 
} 
else { 
    Log.info("element not present"); 
    System.out.println("element not present -- so it entered the else loop");   
} 

下面是HTML標籤:

<li> 
    <a id="logoutLink" href="https://10.4.16.159/index/logout/">Log Out</a> 
</li> 
+1

首先,如果您的方法existsElement()返回布爾值,則不需要將其與_true_相匹配。其次,註銷鏈接顯示在頁面上,當你點擊它時,因爲它似乎出現在某種疊加層上。最後,你爲什麼要做鼠標懸停而不是直接點擊? – Husam

+0

給** existsElement **方法的代碼。我們可以改進它以適合您的情況。 – Husam

回答

0

嘗試使用.size()方法,如果條件:

if(driver.findElements(By.id("logoutLink")).size() != 0){ 

.isEmpty()沿!

if(!driver.findElements(By.id("logoutLink")).isEmpty()){ 
+0

如果我使用上面的代碼獲取一些錯誤。方法size()對於類型By而言是未定義的,否則部分我得到錯誤..請幫助解決此問題..我是新來的硒和java – Amirdha

+0

@阿米爾看到更新的不是'!','.isEmpty()' – Jai

0

嘗試這種情況:

1)

actions.moveToElement(menuHoverLink).perform(); 
menuHoverLink.click; 

insted的的:

actions.moveToElement(menuHoverLink).click().perform(); 
Thread.sleep(6000); 

OR

2)的新方法:

clickWhenTheElementIsClickable(By.id("logoutLink"), 10); 

... 
     protected void clickWhenTheElementIsClickable(By locator, long timeout) { 
      WebDriverWait wait = (WebDriverWait)new WebDriverWait(driver,timeout) 
      .ignoring(StaleElementReferenceException.class); 
      WebElement element = wait.until(
        ExpectedConditions.elementToBeClickable(locator)); 
      element.click(); 
     } 
相關問題