1

I'm a beginner to python, so please be caution with me.

I'm trying to find this on a Website:

<div class="price">$ 1.67<span class="discount">-19.71%</span></div>

I'm using Selenium like this:

#chromedriver
headers = {"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36'}
ser = Service(executable_path='C:\\webdrivers\\chromedriver.exe')
options = Options()
options.page_load_strategy = 'eager'

def Function():
    count = 0
    global driver

    with webdriver.Chrome(service=ser, options=options) as driver:
        driver.get('https://cs.deals/market/csgo/Container/?name=phoeni&sort=price')
        wait = WebDriverWait(driver, 10)
        wait.until(page_has_loaded)

        container = driver.find_element(By.ID, 'price')
        print(container)

    return

I also tried "By.CLASS_NAME" and "By.XPATH"

Exeption-Message: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="price"]"}

Where is my mistake?

I tried to get the first (the lowest) price, but instead I got this exeption.

Do0dl3r
  • 31
  • 4
  • "price" is not an id, it's a class. it should be ```driver.find_element(By.ID, 'price')```. Did you try that and it didn't work? – mrblue6 Mar 02 '23 at 15:26
  • Ah also just noticed in the error message, it says ```"method":"css selector,"selector":"[id="price"]""``` meaning its trying to find the element by searching for css selector. The css selector for this would be ```div.price``` I'm not sure why it is searching via CSS selector, maybe someone else knows why. – mrblue6 Mar 02 '23 at 15:31

1 Answers1

1

To extract the price information ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    driver.get('https://cs.deals/market/csgo/Container/?name=phoeni&sort=price')
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span#cookie-consent-accept"))).click()
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.main-content-margin div.price"))).text)
    
  • Using XPATH:

    driver.get('https://cs.deals/market/csgo/Container/?name=phoeni&sort=price')
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[@id='cookie-consent-accept']"))).click()
    print(WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='main-content-margin']//div[@class='price']"))).text)
    
  • Console Output:

    $ 1.67-19.71%
    
  • Note : You have to add the following imports :

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

You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352