Trying to write code that pulls specific classes from a website using selenium but I am running into issues with duplicate class. The site is kinda set up similar to my example below:
choose grocery store:
div class = current grocery store #The top one is your current grocery store
div class = fruit
div class = fruit
div class = fruit
div class = grocery store
div class = fruit
div class = fruit
div class = fruit
div class = grocery store
div class = fruit
div class = fruit
div class = fruit
div class = grocery store
div class = fruit
div class = fruit
div class = fruit
I just want to grab the fruit from the second grocery store. I tried just printing all the fruit class but its prints everything from all the grocery stores. However I was able to mitigate this by using the following below:
while i <= 4:
driver.implicitly_wait(30)
worldName = driver.find_element(By.XPATH, f"//*[@id='choose-grocery-store']/div[2]/div[2]/div[{i}]/div[2]/div[1]")
driver.implicitly_wait(30)
print(groceryName)
i += 1
This work around works, but I was curious if there was a better way to do it. Next the only issue with my work around is my print states look something like this <selenium.webdriver.remote.webelement.WebElement (session="8cc454e6ac80a174d1f1dec9d324d8c4" and I guess it is just printing the reference? However when I change the code to :
while i <= 4:
driver.implicitly_wait(30)
worldName = driver.find_element(By.XPATH, f"//*[@id='choose-grocery-store']/div[2]/div[2]/div[{i}]/div[2]/div[1]")
driver.implicitly_wait(30)
print(groceryName.text)
i += 1
It breaks