【问题标题】:Selenium testing - checkbox "element not interactable"硒测试 - 复选框“元素不可交互”
【发布时间】:2022-01-15 02:42:07
【问题描述】:

尝试选择元素进行测试。我们有它的 ID,所以:

   @FindBy(how = How.ID, using = "tree-node-home")
    WebElement CheckBoxMenuItem;

抛出错误:"element not interactable".

与 XPath 相同或通过 css 选择 [type='checkbox']

试图推迟加载:

WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.id("tree-node-home")));

但这次我得到了 "java: <identifier> expected" 光标在(ExpectedConditions".之前闪烁

什么鬼?

【问题讨论】:

  • 当您使用FluentWait 而不是WebDriverWait 时是否超时?您可以在 pollingEvery(Duration) 时初始化它 withTimeOut(Duration) 并设置,比如说,30 秒超时?
  • 如果网址是公开的,能发一下吗?
  • 当然,是demoqa.com/checkbox

标签: java selenium testing


【解决方案1】:

您必须单击父标签而不是复选框。

代码:

package selenium;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class DemoQACheckBoxTest extends WebDriverSetup {

    public static void main(String[] args) {

        WebDriver driver = startChromeDriver(); // wrapped driver init
        driver.get("https://demoqa.com/checkbox");
        WebElement checkBox = driver.findElement(By.id("tree-node-home"));
        WebElement checkBoxLabel = driver.findElement(By.xpath("//label[contains(@for,'tree-node-home')]"));
        System.out.println("checkbox text: " + checkBox.getText());
        System.out.println("label text: " + checkBoxLabel.getText());
        System.out.println("checkbox is displayed: " + checkBox.isDisplayed());
        System.out.println("checkbox is enabled: " + checkBox.isEnabled());
        System.out.println("checkbox is selected: " + checkBox.isSelected());
        checkBoxLabel.click();
        System.out.println("checkbox is selected: " + checkBox.isSelected());
        driver.quit();
    }

}

输出:

Starting ChromeDriver 96.0.4664.45 (76e4c1bb2ab4671b8beba3444e61c0f17584b2fc-refs/branch-heads/4664@{#947}) on port 16990
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Pro 10, 2021 2:23:39 ODP. org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
checkbox text: 
label text: Home
checkbox is displayed: false
checkbox is enabled: true
checkbox is selected: false
checkbox is selected: true

【讨论】:

  • 是的,没错!谢谢!
猜你喜欢
  • 1970-01-01
  • 2019-09-30
  • 2021-08-06
  • 1970-01-01
  • 1970-01-01
  • 2020-04-27
  • 1970-01-01
  • 2019-09-22
  • 1970-01-01
相关资源
最近更新 更多