Reputation: 25
My scenario is that when I clicked on Samsung checkbox it loads for half a sec. Then after I have to click on Capacity and click on one of the options(checkbox).
I need to perform the task using WebDriverWait and not thread.sleep().
When i use this statement it does nothing
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Whirlpool')]")));
List<WebElement> filterOnProduct = driver.findElements(By.xpath("(//div[@class='_3FPh42'])[1]/div/div"));
for(int i=0; i<filterOnProduct.size();i++) {
System.out.println(filterOnProduct.get(i).getText());
String fridge = filterOnProduct.get(i).getText();
if(fridge.contains("SAMSUNG")) {
filterOnProduct.get(i).click();
break;
}
}
// Thread.sleep(1000);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Whirlpool')]")));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,250)");
WebElement capacity = driver.findElement(By.xpath("//div[contains(text(),'Capacity')]"));
action = new Actions(driver);
action.moveToElement(capacity).click().build().perform();
Upvotes: 0
Views: 141
Reputation: 511
@Codeception See the following code. I have given the explanation within my code as comments.
WebDriverWait wait = (new WebDriverWait(driver, Duration.ofSeconds(10)));
driver.get(
"https://www.flipkart.com/search?q=fridge&otracker=search&otracker1=search&marketplace=FLIPKART&as-show=on&as=off&as-pos=1&as-type=HISTORY");
// Waiting for the visibility of Samsung Brand before clicking it.
WebElement samsung = wait.until(ExpectedConditions.visibilityOfElementLocated(
By.xpath("//div[text()='Brand']/parent::div//following-sibling::div//div[@title='SAMSUNG']")));
samsung.click();
// Since the page is getting refreshed after above action, to avoid the
// StaleElemenetReferenceException,
// waiting for Samsung checkbox to be selected.
wait.until(ExpectedConditions.elementSelectionStateToBe(
By.xpath("//div[text()='Brand']/parent::div//following-sibling::div//div[@title='SAMSUNG']//input"),
true));
// By the time above action happens, Capacity is already available so not
// waiting it.
WebElement capacity = driver.findElement(By.xpath("//div[text()='Capacity']/parent::div"));
capacity.click();
// Waiting for the visibility of a particular option to be visible.
// In the below case waiting for visibility of "251 - 300 L" option
WebElement specificCapacity = wait.until(ExpectedConditions.visibilityOfElementLocated(
By.xpath("//div[text()='Capacity']/parent::div//following-sibling::div//div[@title='251 - 300 L']")));
specificCapacity.click();
Upvotes: 1
Reputation: 67
Your wait statement is not working because even during the loading time of half a second the element you used to wait for visibility is visible. For proper wait delays, I would suggest you use any option that was not available during the load time.
For example, in your case, the highlighted 'SAMSUNG' text in the Filter section was not available during the load time. So if you wait for the visibility of that element, it may work.
Please give the below statement a try:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='Filters']/ancestor::section//*[text()='SAMSUNG']")));
Upvotes: 1