2017-02-01 4 views
-1

リストされたメソッドを使用してページ上に要素が表示されているかどうかを確認すると、指定されたロケータを使用して要素を見つけることができないという例外が発生しました()。セレンメソッドvisibilityOf - 動作していないようですか?

これまでにこの問題に直面したことがありましたか、より良い方法がありますか?更新

public boolean isElementPresentByWebElement(WebElement element) { 
    Wait<WebDriver> fluentWait = new FluentWait<WebDriver>(driver).withTimeout(15, TimeUnit.SECONDS) 
      .pollingEvery(1, TimeUnit.SECONDS).ignoring(NoSuchElementException.class); 

    for (int i = 0; i < 2; i++) { 
     try { 
      fluentWait.until(ExpectedConditions.visibilityOf(element)); 
      System.out.println("Element is visible: " + element.toString()); 
      return true; 
     } catch (Exception e) { 
      System.out.println(
        "Unable to locate the element: " + element.toString() + ", Exception: " + e.toString()); 
      throw (e); 
     } 
    } 
    return false; 
} 

enter image description here

+0

htmlコードが – Gbru

+0

@Anderssonを貼ら見つけてください@andersonは、htmlコードを見つけてください、あなたは私はあなたが輸入している例外を知らせることができます – Gbru

+0

を貼ら。 org.openqa.selenium.NoSuchElementExceptionである必要があります。 –

答えて

1

私はあなたのコードが何をしようとするために過度に複雑だと思います。あなたが望むことをするクラス、ExpectedConditionsがあります。あなたはまた不要な待機をループしています。 WebElementの代わりにロケータ(By)を渡すことをお勧めします。この関数を使用する前に要素を見つける必要がないため、この関数の使用を拡張します。

public boolean isElementPresentByLocator(By locator) 
{ 
    try 
    { 
     new WebDriverWait(driver, 15).until(ExpectedConditions.visibilityOfElementLocated(locator)); 
     System.out.println("Element is visible: " + locator.toString()); 
     return true; 
    } 
    catch (TimeoutException e) 
    { 
     System.out.println("Unable to locate the element: " + locator.toString() + ", Exception: " + e.toString()); 
     return false; 
    } 
} 

以下のコードは、コードの直接的な翻訳と簡略化の詳細です。

public boolean isElementPresentByWebElement(WebElement element) 
{ 
    try 
    { 
     new WebDriverWait(driver, 15).until(ExpectedConditions.visibilityOf(element)); 
     System.out.println("Element is visible: " + element.toString()); 
     return true; 
    } 
    catch (TimeoutException e) 
    { 
     System.out.println("Unable to locate the element: " + element.toString() + ", Exception: " + e.toString()); 
     return false; 
    } 
} 
-1

は以下を使用してみてください:

int waitCounter = 0; 

public static void WaitUntilVisible(WebDriver driver, WebElement element) throws InterruptedException, IOException { 
try { 


    WebDriverWait wait = new WebDriverWait(driver, 20); 


    wait.until(ExpectedConditions.visibilityOf(elementToBeClicked)); 
    if (!elementToBeClicked.isDisplayed()) { 

     System.out.println("Element not visible yet. waiting some more for " + element); 
     if (waitCounter < 3) { 
      waitCounter++; 
      WaitUntilVisible(element); 
     } 
     waitCounter = 0; 
    } 

} catch (Exception e) 

{ 
    System.out.println("Handling exception"); 
} 
} 
+0

このコードはコンパイルされません。いくつかの宣言されていない変数を使用しており、定義されていない 'Logger_Info()'を導入しました。 – JeffC

+0

'waitcounter = 0'を初期化し、 'logger_info'の代わりに 'System.our.println'を使用するだけです。 – kushal

関連する問題