#๐Ÿ”’ Pull specific classes in Selenium when there are duplicates, and issues with printing?

5 messages ยท Page 1 of 1 (latest)

teal wyvern
#

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

balmy yarrowBOT
#

@teal wyvern

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

teal wyvern
#

Fix by doing this:

  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]").text
        driver.implicitly_wait(30)
        print(groceryName)
        i += 1
balmy yarrowBOT
#

@teal wyvern

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.