In this website https://rahulshettyacademy.com/seleniumPractise/#/. I filtered vegetables starting with CA and trying to get only displayed filtered items from the page. if I try the locators manually after the filter is giving only 4 instances same as if try with the script it is taking all the 30 items from the DOM.
JavaScript
x
4
1
driver.find_element_by_css_selector("input[type='search']").send_keys("ca") #SearchBox
2
veg_name = driver.find_elements_by_css_selector("div.products-wrapper div h4") # filtered Items
3
print(len(veg_name))
4
Advertisement
Answer
The simplest way is to add some delay (wait) after sending the text to search input and get the amount of displayed results after that.
1 second delay will be more than enough.
To do it more correct but by the longer code you can and
JavaScript
1
2
1
wait.until((ExpectedConditions.invisibilityOfElementLocated(element)))
2
where element
is locator of product that should disappear in your search
just add some short delay after that case to insure all the other irrelevant elements disappeared too.