Issue
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");
}
I'm trying to automate https://demoqa.com/checkbox clicked on toggle button of Home and tried to click Desktop it's throwing an exception as below Getting below:-
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)
Solution
Root cause of the issue: You are waiting for the Desktop
toggle to be visible and also you are capturing the element into a WebElement
variable, however you forgot to click()
on it. Since, you did not click on the Desktop toggle, Notes
element was not visible. Hence below exception:
Expected condition failed: waiting for visibility of element located by By.xpath: //span[text()='Notes']//ancestor::label
Solution: Just click on the toggle as below so that Notes
element is visible.
WebElement Desktop = d.findElement(By.xpath("//span[text()='Desktop']/../../button"));
Desktop.click();
UPDATE: I have refactored your code and removed unnecessary Explicit Waits
. Check the below code:
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");
}
Output:
All clicked
Only Notes clicked
Only Commands clicked
Answered By - Shawn
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.