Skip to content
Advertisement

How to get the filtered items alone from webpage using selenium python?

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.

driver.find_element_by_css_selector("input[type='search']").send_keys("ca")  #SearchBox   
veg_name = driver.find_elements_by_css_selector("div.products-wrapper div h4") # filtered Items   
print(len(veg_name))

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

wait.until((ExpectedConditions.invisibilityOfElementLocated(element))) 

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.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement