嘗試選擇元素進行測驗。我們有它的 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".
什么F。?
uj5u.com熱心網友回復:
您必須單擊父標簽而不是復選框。
代碼:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/385315.html
