如何使用Selenium WebDriver Java点击ElementNotInteractiableExceptio



我想点击复选框,但它一直说元素是不可交互的。我实际上试过了.click、javascriptexecutor、actions、wait until、thread.sleep、scroll。即使我尝试上下文点击复选框,它也会显示该元素不可交互。还有其他想法吗?

<div class="panel panel-default">
<div class="panel-heading">Single Checkbox Demo</div>
<div class="panel-body">
<p> Clicking on the checkbox will display a success message. Keep an eye on it</p>
<div class="checkbox">
<label>
<input type="checkbox" id="isAgeSelected" value="">Click on this check box</label>
</div>
<div id="txtAge" style="display:none">Success - Check box is checked</div>
</div>
</div>

这是网址:https://www.seleniumeasy.com/test/basic-checkbox-demo.html

复选框代码:

@FindBy(id = "isAgeSelected")
private WebElement checkboxFirstExample;
public checkboxDemo checkboxFirstEx(){
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].scrollIntoView();", checkboxFirstExample);
executor.executeScript("arguments[0].click();", checkboxFirstExample);
return this;
}

完整代码:

public class checkboxDemo extends baseSeleniumClass {
@FindBy(xpath = "//ul[@id='treemenu']//a[contains(text(),'Input Forms')]")
private WebElement inputFormsMenu;
@FindBy(xpath = "//li[@style='display: list-item;']//a[@href='./basic-checkbox-demo.html']")
private WebElement checkboxDemo;
@FindBy(id = "isAgeSelected")
private WebElement checkboxFirstExample;
@FindBy(xpath = "//div[@id='txtAge']")
private WebElement ageSelected;
@FindBy(xpath = "//input[@type='button'][@id='check1']")
private WebElement checkboxAll;
private seleniumHelper helper;
private WebDriver driver;
public checkboxDemo(WebDriver driver){
PageFactory.initElements(driver, this);
this.helper = new seleniumHelper(driver);
this.driver = driver;
}
public checkboxDemo checkboxDemostart(){
checkboxDemo.click();
return this;
}
public checkboxDemo checkboxFirstEx(){
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].scrollIntoView({block: 'center'});", checkboxFirstExample);
checkboxFirstExample.click();
System.out.println("3. " + ageSelected.getText() + " - First checkbox passed");
return this;
}
public checkboxDemo checkboxSecondEx(){
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].scrollIntoView();", checkboxAll);
executor.executeScript("arguments[0].click();", checkboxAll);
System.out.println("4. " + checkboxAll.getAttribute("value") + " - All checkboxes are selected");
return this;
}
public checkboxDemo inputMenu(){
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", inputFormsMenu);
return this;
}

}

@FindBy(id = "isAgeSelected")
private WebElement checkboxFirstExample;
public checkboxDemo checkboxFirstEx(){
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].scrollIntoView({block: "center"});", checkboxFirstExample);
checkboxFirstExample.click();
return this;
}

使用id"isAgeSelected"。你应该点击输入标签

xpath:

//input[@id="isAgeSelected"]

CSS:

input[id="isAgeSelected"]

最新更新