Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.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
org.openqa.selenium.ElementNotVisibleException:通过SeleniumWebDriver和Java单击复选框时,元素当前不可见_Java_Selenium_Xpath_Css Selectors_Webdriverwait - Fatal编程技术网

org.openqa.selenium.ElementNotVisibleException:通过SeleniumWebDriver和Java单击复选框时,元素当前不可见

org.openqa.selenium.ElementNotVisibleException:通过SeleniumWebDriver和Java单击复选框时,元素当前不可见,java,selenium,xpath,css-selectors,webdriverwait,Java,Selenium,Xpath,Css Selectors,Webdriverwait,我必须选中一个复选框,该复选框包含在下面的HTML代码段中。 复选框包含在输入标记中 资助者 尝试使用javascript单击: public void clickElementWithJS(By locator) { String jsClickCode = "arguments[0].scrollIntoView(true); arguments[0].click();"; try { WebElement elementToClick = driver

我必须选中一个复选框,该复选框包含在下面的HTML代码段中。 复选框包含在输入标记中


资助者

尝试使用javascript单击:

public void clickElementWithJS(By locator) {
    String jsClickCode = "arguments[0].scrollIntoView(true); arguments[0].click();";
    try {
        WebElement elementToClick = driver.findElement(locator);
        ((JavascriptExecutor) driver).executeScript(jsClickCode, elementToClick);
    } catch(Exception e) {
        System.out.println("Element could not be clicked.. "  + e.getMessage());
    }
}

此完整的错误消息是

Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 2.05 seconds
…表示当WebDriver实例试图查找所需的元素时,该元素在中不可见


ElementNotVisibleException 抛出,以指示虽然元素存在于上,但它不可见,因此无法与交互。它是运行时异常,具有以下层次结构:

java.lang.RuntimeException
    org.openqa.selenium.WebDriverException
        org.openqa.selenium.InvalidElementStateException
            org.openqa.selenium.ElementNotInteractableException
                org.openqa.selenium.ElementNotVisibleException

现场总结 此异常的字段继承自类org.openqa.selenium。详情如下:

Modifier and Type                        Field and Description
---------------------------------        ---------------------
protected static java.lang.String        BASE_SUPPORT_URL 
static java.lang.String                  DRIVER_INFO 
static java.lang.String                  SESSION_ID 
//using id attribute
new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.id("element_id"))).getAttribute("innerHTML");
//using linkText attribute          
new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.linkText("element_linkText"))).getAttribute("innerHTML");
//using cssSelector     
new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("element_cssSelector"))).getAttribute("innerHTML");
//using xpath           
new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("element_xpath"))).getAttribute("innerHTML");
//using id attribute
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.id("element_id"))).click();
//using linkText attribute          
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.linkText("element_linkText"))).click();
//using cssSelector     
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("element_cssSelector"))).click();
//using xpath           
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("element_xpath"))).click();

理由 ElementNotVisibleException的一个可能的例外是,WebElement在HTML中是存在的,当尝试
单击()
读取隐藏在视图中的元素属性时,通常会遇到此异常


解决方案 As ElementNotVisibleException确保WebElement在HTML中存在,因此前面的解决方案将是两个方面,如下所述:

  • 如果下一步是读取所需元素的任何属性,则需要结合子句进行归纳,如下所示:

    Modifier and Type                        Field and Description
    ---------------------------------        ---------------------
    protected static java.lang.String        BASE_SUPPORT_URL 
    static java.lang.String                  DRIVER_INFO 
    static java.lang.String                  SESSION_ID 
    
    //using id attribute
    new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.id("element_id"))).getAttribute("innerHTML");
    //using linkText attribute          
    new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.linkText("element_linkText"))).getAttribute("innerHTML");
    //using cssSelector     
    new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("element_cssSelector"))).getAttribute("innerHTML");
    //using xpath           
    new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("element_xpath"))).getAttribute("innerHTML");
    
    //using id attribute
    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.id("element_id"))).click();
    //using linkText attribute          
    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.linkText("element_linkText"))).click();
    //using cssSelector     
    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("element_cssSelector"))).click();
    //using xpath           
    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("element_xpath"))).click();
    
  • 如果下一步是在所需元素上调用
    click()
    ,则需要结合将子句设置为以下内容进行归纳:

    Modifier and Type                        Field and Description
    ---------------------------------        ---------------------
    protected static java.lang.String        BASE_SUPPORT_URL 
    static java.lang.String                  DRIVER_INFO 
    static java.lang.String                  SESSION_ID 
    
    //using id attribute
    new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.id("element_id"))).getAttribute("innerHTML");
    //using linkText attribute          
    new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.linkText("element_linkText"))).getAttribute("innerHTML");
    //using cssSelector     
    new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("element_cssSelector"))).getAttribute("innerHTML");
    //using xpath           
    new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("element_xpath"))).getAttribute("innerHTML");
    
    //using id attribute
    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.id("element_id"))).click();
    //using linkText attribute          
    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.linkText("element_linkText"))).click();
    //using cssSelector     
    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("element_cssSelector"))).click();
    //using xpath           
    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("element_xpath"))).click();
    

这个用例 所需元素是一个元素,因此您需要引导WebDriverWait使该元素可单击,并且您可以使用以下任一解决方案:

  • css选择器

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("span.tix-checkbox input.ng-star-inserted[name='undefined']"))).click();
    
  • xpath

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[@class='tix-checkbox']//input[@class='ng-star-inserted' and @name='undefined']"))).click();