Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Selenium Webdriver元素不存在条件失败_Java_Selenium_Webdriver - Fatal编程技术网

Java Selenium Webdriver元素不存在条件失败

Java Selenium Webdriver元素不存在条件失败,java,selenium,webdriver,Java,Selenium,Webdriver,我在一个while块内有一个If-Else块。如果元素存在,请单击它以将其删除并将其放回父列表。否则,如果元素不在列表中,则从父列表中选择并将其放回 它第一次起作用。它看到元素存在,单击它以删除它。第二次检查元素时失败 我试过用FindElement.isplayed和=空 我得到一个例外: org.openqa.selenium.NoSuchElementException: Unable to find element with css selector == select[id="idSe

我在一个while块内有一个If-Else块。如果元素存在,请单击它以将其删除并将其放回父列表。否则,如果元素不在列表中,则从父列表中选择并将其放回

它第一次起作用。它看到元素存在,单击它以删除它。第二次检查元素时失败 我试过用FindElement.isplayed和=空

我得到一个例外:

org.openqa.selenium.NoSuchElementException: Unable to find element with css selector == select[id="idSelSelectedLanes"]>option[value="9012"] (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 30.16 seconds
我错过了什么

这是我在这里的第一篇帖子,所以对于任何格式问题,我深表歉意

谢谢

count ++;
if(count % 2 == 0){  
    if(BROWSER.equals("IE")) {
       // check if 9012 is present
        if(driver.findElement(By.cssSelector("select[id=\"idSelSelectedLanes\"]>option[value=\"9012\"]"))!=null){
            try {
               // since its present, click to remove
               driver.findElement(By.cssSelector("select[id=\"idSelSelectedLanes\"]>option[value=\"9012\"]")).click();;
               Thread.sleep(1000);
            } catch(NoSuchElementException e) {
               System.out.println("Couldn't remove 9012");
            }
        } else  {
            try {
               //Not present, so select from Available Lanes    
               driver.findElement(By.cssSelector("select[id=\"idSelAvailableLanes\"]>option[value=\"9012\"]")).isDisplayed();
            } catch (NoSuchElementException e) {
               System.out.println("Couldn't add 9012");                            
            }
        }
    }
}

您需要将
driver.findElement(…)
放入
try catch
块中

count ++;
WebElement e;
if(count % 2 == 0) {
    if(BROWSER.equals("IE")) {
        // check if 9012 is present
        try {
            e = driver.findElement(By.cssSelector("select[id=\"idSelSelectedLanes\"]>option[value=\"9012\"]"));
            Thread.sleep(1000);
            e.click()
        } catch (NoSuchElementException e) {
            System.out.println("Couldn't remove 9012");
            // the else part goes here
        }
    }
}


另一种方法是使用
findElement
而不是
findElement
来避免try-catch,并使用
.get(0)
来获取所需的元素。

另一种解决方案,您应该首先使用findElements检查elementExist,如果它存在->执行其他操作

count ++;
WebElement e;
String e9012Css = "select[id=\"idSelSelectedLanes\"]>option[value=\"9012\"]";
if(count % 2 == 0) {
    if(BROWSER.equals("IE")) {
    // check if 9012 is present
        e9012Existed = driver.findElements(By.cssSelector(e9012Css)).size() > 0;
        if(e9012Existed) {
            driver.findElement(By.cssSelector(e9012Css)).Click();
        } 
    }
    else {
        System.out.println("Couldn't remove 9012");
    }
}

尝试使用isElementPresent

if(isElementPresent(By.cssSelector("select[id=\"idSelSelectedLanes\"]>option[value=\"9012\"]"))){
  // since its present, click to remove 
} else {
  //Not present, so select from Available Lanes 
}

谢谢那有帮助