代码之家  ›  专栏  ›  技术社区  ›  AnOldSoul

元素不可见,但复选框可用

  •  1
  • AnOldSoul  · 技术社区  · 7 年前

    我正在尝试使用Selenium勾选一个复选框。页面如下所示。

    enter image description here

    enter image description here

    此时,我可以使用下面显示的xpath从Chrome开发工具中选择复选框。

    //input[@name='value[326071]']
    

    下面是DOM的外观。

    enter image description here

    我已在单击复选框之前放置了一个线程等待。下面是我的代码。

    Thread.sleep(5000);
    WebElement assignWorkSpaceElement = chromeDriver.findElement(By.xpath("//input[@name='value[326071]']"));
    assignWorkSpaceElement.click();
    

    我也尝试了下面的xpath。还是不走运。我正在通过IntelliJ调试代码,在浏览web元素行之前,我能够从Chrome开发工具中选择复选框。它显然在那里。

    //*[contains(@class,'col-md-8')]//input[@name='value[326071]']
    

    org.openqa.selenium.ElementNotVisibleException: element not visible
      (Session info: chrome=70.0.3538.110)
      (Driver info: chromedriver=2.40.565386 (45a059dc425e08165f9a10324bd1380cc13ca363),platform=Mac OS X 10.13.6 x86_64) (WARNING: The server did not provide any stacktrace information)
    Command duration or timeout: 0 milliseconds
    

    2 回复  |  直到 7 年前
        1
  •  2
  •   Moshe Slavin    7 年前

    据我在HTML中看到的,您正在查找的XPath似乎有多个元素。。。

    现在,如果您尝试循环所有复选框并单击,可以通过以下方法完成:

    WebElement assignWorkSpaceElement = chromeDriver.findElements(By.xpath("//input[@name='value[326071]']"));
    for (WebElement el : assignWorkSpaceElement ) {
         el.click();
    }
    

    但是,如果只需要值为326071的一个元素,则必须添加一些更具体的XPath,如:

    WebElement assignWorkSpaceElement = chromeDriver.findElement(By.xpath("//input[@name='value[326071]' and @type='checkbox' ]"));
    assignWorkSpaceElement.click();
    

    WebElement assignWorkSpaceElement = chromeDriver.findElement(By.xpath("//input[@name='value[326071]' and @value='1' ]"));
    assignWorkSpaceElement.click();
    

    希望这是有益的!

        2
  •  1
  •   koushick    7 年前

    睡眠 而不是那种用途 webdriver等待。

    WebDriverWait wait = new WebDriverWait(driver, 5);
      WebElement e4 = wait.until(ExpectedConditions.visibilityOf(By.xpath("//div[@class='col-md-8- form-control-static']//following::input[1]")));
    
    if(e4.isDisplayed())
    {
      e4.click();
    }
    
    推荐文章