2017-03-07 53 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(); 
相关问题