Cant get bot to select item off of supreme

Viewed 66

Im a beginner to python btw.

My goal is to build a very simple ACO bot for supreme. Although im running into a problem and I have not had much luck with solutions. I tried to use driver.find_element(By.XPATH, "") but this is not exactly possible since the website constantly changes and I would need to always change the name of the product I want.

I tried to use "title", "ID" or "class" but there are multiple items with the same class, title and ID so that does not work. I read that someone doing the same thing as I, was using "PARTIAL_LINK_TEXT" so I tried driver.find_element(By.PARTIAL_LINK_TEXT, "Boxer Briefs"). I tried this by putting in "Boxer Briefs" to see if it would select the "Supreme/Hanes Boxer Briefs as a test. Unfortunately this also did not work, and it give me error saying

selenium.common.exceptions.NoSuchElementException: Message:

this is all of my code, like I said im a beginner and this is a very basic bot.

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Safari()

url = "https://www.supremenewyork.com/shop/all"

driver.get(url)

driver.maximize_window()

click = driver.find_element(By.XPATH, '//*[@id="nav-categories"]/li[10]/a')

click.click()

element = driver.find_element(By.PARTIAL_LINK_TEXT, "Boxer Briefs")

any help would be really appreciated.

https://www.supremenewyork.com/shop/all

1 Answers

JavaScript needs some time to replace elements - when I use time.sleep(1) after click() then it works without problems.

from selenium import webdriver
from selenium.webdriver.common.by import By
import time

#driver = webdriver.Safari()
driver = webdriver.Firefox()
driver.maximize_window()

url = "https://www.supremenewyork.com/shop/all"
driver.get(url)

click = driver.find_element(By.XPATH, '//*[@id="nav-categories"]/li[10]/a')
click.click()

time.sleep(1)

element = driver.find_element(By.PARTIAL_LINK_TEXT, "Boxer Briefs")
print(element.text)

See also Selenium doc: Waits to create more universal code.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

#driver = webdriver.Safari()
driver = webdriver.Firefox()
driver.maximize_window()

url = "https://www.supremenewyork.com/shop/all"
driver.get(url)

click = driver.find_element(By.XPATH, '//*[@id="nav-categories"]/li[10]/a')
click.click()

try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, "Boxer Briefs"))
    )
    print(element.text)
except Exception as ex:    
    print('Exception:', ex)

Result:

Supreme®/Hanes® Boxer Briefs (2 Pack)

Tested with Firefox and Chrome on Linux Mint 20 (based on Ubuntu 21.04)


EDIT:

If I presence_of_all_elements_located instead of presence_of_element_located

try:
    all_elements = WebDriverWait(driver, 10).until(
        EC.presence_of_all_elements_located((By.PARTIAL_LINK_TEXT, "Boxer Briefs"))
    )
    for element in all_elements:
        print(element.text)
except Exception as ex:    
    print('Exception:', ex)

the result is

Supreme®/Hanes® Boxer Briefs (2 Pack)
Supreme®/Hanes® Boxer Briefs (4 Pack)
Supreme®/Hanes® Boxer Briefs (4 Pack)
Related