2017-03-07 34 views
0

boolean display = driver.findElement(By.cssSelector(「input#txtkeyword [placeholder ='Job title']」 ))。isDisplayed(); = false boolean select = driver.findElement(By.cssSelector(「input#txtkeyword [placeholder ='Job title']」))。isSelected(); = false boolean enable = driver.findElement (By.cssSelector(「input#txtkeyword [placeholder ='Job title']」))。isEnabled(); = trueorg.openqa.selenium.ElementNotVisibleException:元素不可見/嘗試使用ID以及CSS選擇器的其他組合

+0

最初我嘗試使用ID,但是當我找到driver.findElement(By(ID))。size()> 1時,我將其更改爲CSS選擇器。 – Gazal

+0

是否可以請您分享HTML代碼段,這將有助於識別問題。 –

+0

Gazal

回答

0

元素不可見的原因很多。它可能被彈出窗口覆蓋,DOM仍可能正在加載,您可能需要將其滾動到視圖中。

對於第一個實例,請對失敗截圖並查看是否覆蓋了該元素。我使用下面的cucumber-jvm。你可以谷歌如何做到這一點,無論你使用的任何框架。

@After 
public void captureScreenshotOnFailure(Scenario scenario){ 
    try { 
     if (scenario.isFailed() && driver !=null) { 
      System.out.println("***>> Scenario failed: "+scenario.getStatus()); 
      try { 
       driver augemented = new Augmenter().augment(webDriver); 
       byte[] screenshot = ((TakesScreenshot) augemented).getScreenshotAs(OutputType.BYTES); 

       scenario.embed(screenshot, "image/png"); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 

    } finally { 
     if (driver !=null) { 
      driver.quit(); 
     } 
    } 
} 

對於DOM未完成加載,請等待它。

Wait<driver> wait_element = new WebDriverWait(driver, 80); 
WebElement jobTitleElement = wait_element.until(
    ExpectedConditions.visibilityOfElementLocated(By.cssSelector(
    "input#txtkeyword[placeholder='Job title']"))); 

如果等待失敗,那麼該元素就不存在了。

如果等待成功,則滾動到該元素。如果元素是一個按鈕,您可以在moveToElement()之後單擊()它。這只是包括代碼才完整。

Actions action = new Actions(driver); 
action.moveToElement(jobTitleElement).click().build().perform(); 
相關問題