Search code examples
pythonseleniumxpathcss-selectorswebdriverwait

Selenium select element which isnt directly in the page source in python


For instance I have this website: https://skinport.com/item/stattrak-usp-s-black-lotus-minimal-wear/6128018 and want to get the current price of the item. Selenium doesn't find the element by class name, XPath or css selector. I think that's just because the page source doesn't have the price. The site consists of a few scripts which prints the current price

So I have something like this in python:

driver.get("https://skinport.com/item/stattrak-usp-s-black-lotus-field-tested/6196388")
print(price = driver.find_element(By.XPATH, '//*[@id="content"]/div[1]/div[2]/div/div/div[2]/div[1]/div'))

And I get this error: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element

With

print(driver.find_elements(By.CSS_SELECTOR("#content > div.ItemPage > div.ItemPage-column.ItemPage-column--right > div:nth-child(1) > div > div.ItemPage-price > div.ItemPage-value > div")))

I get this error: TypeError: 'str' object is not callable


Solution

  • You are missing a wait.
    You should let the page loaded before accessing that element.
    The preferred way to do that is to use the expected conditions explicit waits.
    Also you are missing the text method to retrieve the text from the web element.
    Also, your locator is bad.
    Something like this should work:

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    driver.get("https://skinport.com/item/stattrak-usp-s-black-lotus-field-tested/6196388")
    wait = WebDriverWait(driver, 20)
    price = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.ItemPage-price div.Tooltip-link"))).text
    print(price)