You can get all the products by searching for the div tags that contain the attribute data-content="productItem". That is assuming each item has that attribute. From the screenshots you posted, it seems like that is the case.
You can accomplish this using find_elements_by_xpath()
for item in driver.find_elements_by_xpath('//div[@data-content="productItem"]'):
....
This would probably be the best way without having to worry about the elements having different css classes.
Answer from 010011100101 on Stack OverflowHi all. I'm trying to use Selenium to find an element by CSS selector and although I've tried using the documentation, I think I'm misunderstanding something.
This is what I'm trying:
driver.find_element(By.CSS_SELECTOR,f"img [src='images/icons/new.gif'][alt='Add new item to list']").click()
This is the element I want to find and click:
<img src="images/icons/new.gif" alt="Add new item to list">
The error I get is as follows:
Exception has occurred: NoSuchElementException
Message: no such element: Unable to locate element: {"method":"css selector","selector":"img [src='images/icons/new.gif'][alt='Add new item to list']"}I can see it's on the page, however, so I'm not sure what I'm missing. I can find other items using XPATH and ID so selenium is up and running, just to rule that out. Can anybody suggest anything?
(I'm using selenium 4.8.2 and python 3.11.1, if that helps)