Trying to select element for testing. We have it's ID, so:
@FindBy(how = How.ID, using = "tree-node-home")
WebElement CheckBoxMenuItem;
Throws error: "element not interactable".
Same with XPath or selecting by css [type='checkbox']
Tried to defer loading:
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.id("tree-node-home")));
But this time I got
"java: <identifier> expected"
with the cursor blinking before (ExpectedConditions".
What the F.?
CodePudding user response:
You have to click the parrent label instead of the checkbox.
Code:
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();
}
}
Output:
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