public class ProgramDemoQA
{
public static WebDriver d;
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver",
"D:\\RamanaSoft\\PracticeCoding\\DemoQA\\drivers\\chromedriver.exe");
d = new ChromeDriver();
d.get("https://demoqa.com");
d.manage().timeouts().implicitlyWait(Duration.ofSeconds(20));
d.manage().window().maximize();
d.findElement(By.xpath("//h5[text()='Elements']/../..")).click();
d.findElement(By.xpath("//span[text()='Check Box']/..")).click();
Thread.sleep(5000);
d.findElement(By.xpath("//button[@aria-label=\'Toggle\']")).click();
WebDriverWait w = new WebDriverWait(d,Duration.ofSeconds(10));
w.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='Desktop']/../../button")));
WebElement Desktop = d.findElement(By.xpath("//span[text()='Desktop']/../../button"));
WebDriverWait w1 = new WebDriverWait(d,Duration.ofSeconds(60));
w1.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(
"//span[text()='Notes']//ancestor::label")));
WebElement Notes = d.findElement(By.xpath("//span[text()='Notes']//ancestor::label"));
WebDriverWait w2 = new WebDriverWait(d,Duration.ofSeconds(50));
w2.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(
"//span[text()='Commands']//ancestor::label")));
WebElement Commands= d.findElement(By.xpath("//span[text()='Commands']//ancestor::label"));
if(Desktop.isDisplayed())
{
Desktop.click();
//Notes.click();
Commands.click();
System.out.println("All clicked");
}
if(Desktop.isDisplayed())
{
//Desktop.click();
Commands.click();
System.out.println("Only Notes clicked");
}
if(Desktop.isDisplayed())
{
//Desktop.click();
// Notes.click();
System.out.println("Only Commands clicked");
}
我正在尝试自动化https://demoqa.com/checkbox 单击主页的切换按钮并尝试单击桌面,它抛出异常,如下所示 下面:-
Expected condition failed: waiting for visibility of element located by By.xpath: //span[text()='Notes']//ancestor::label (tried for 60 second(s) with 500 milliseconds interval)
问题的根本原因:您正在等待
Desktop
切换可见,并且正在将元素捕获到WebElement
变量中,但是您忘记了click()
。由于您没有单击桌面切换,因此 Notes
元素不可见。因此以下例外:
Expected condition failed: waiting for visibility of element located by By.xpath: //span[text()='Notes']//ancestor::label
解决方案: 只需单击如下所示的开关,即可看到
Notes
元素。
WebElement Desktop = d.findElement(By.xpath("//span[text()='Desktop']/../../button"));
Desktop.click();
更新:我重构了您的代码并删除了不必要的
Explicit Waits
。检查以下代码:
WebDriver d = new ChromeDriver();
d.get("https://demoqa.com");
d.manage().timeouts().implicitlyWait(Duration.ofSeconds(20));
d.manage().window().maximize();
// below line will click on Consent pop-up, if you do not get this pop-up just remove the line
d.findElement(By.xpath("//p[text()='Consent']")).click();
d.findElement(By.xpath("//h5[text()='Elements']/../..")).click();
d.findElement(By.xpath("//span[text()='Check Box']/..")).click();
d.findElement(By.xpath("//button[@aria-label=\'Toggle\']")).click();
WebElement Desktop = d.findElement(By.xpath("//span[text()='Desktop']/../../button"));
Desktop.click();
WebElement Notes = d.findElement(By.xpath("//span[text()='Notes']//ancestor::label"));
WebElement Commands= d.findElement(By.xpath("//span[text()='Commands']//ancestor::label"));
if(Desktop.isDisplayed())
{
Notes.click();
Commands.click();
System.out.println("All clicked");
// below line will un-check Commands checkbox
Commands.click();
System.out.println("Only Notes clicked");
// below 2 lines will un-check Notes checkbox and check Commands checkbox
Notes.click();
Commands.click();
System.out.println("Only Commands clicked");
}
输出:
All clicked
Only Notes clicked
Only Commands clicked