2

I get this error when trying to get the price of the product:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element

But the thing is that I am searching by XPath from the Inspect HTML panel. SO how come it doesn't'search it?

Here is the code:

main_page = 'https://www.takealot.com/500-classic-family-computer-tv-game-ejc/PLID90448265'
PATH = 'C:\Program Files (x86)\chromedriver.exe'
driver = webdriver.Chrome(PATH)
driver.get(main_page)
time.sleep(10)
active_price = driver.find_element(By.XPATH,'//*[@id="shopfront-app"]/div[3]/div[1]/div[2]/aside/div[1]/div[1]/div[1]/span').text
print(active_price)

I found the way to get the price the other way, but I am still interested why selenium can't find it by XPath:

price = driver.find_element(By.CLASS_NAME, 'buybox-module_price_2YUFa').text

active_price = ''
after_discount = ''
count = 0

for char in price:
    if char == 'R':
        count +=1
    if count ==2:
        after_discount += char
    else:
        active_price += char

print(active_price)
print(after_discount)
densol96
  • 49
  • 3

2 Answers2

1

To extract the text R 345 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 and text attribute:

    driver.get("https://www.takealot.com/500-classic-family-computer-tv-game-ejc/PLID90448265")
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span[data-ref='buybox-price-main']"))).text)
    
  • Using XPATH and get_attribute("innerHTML"):

    driver.get("https://www.takealot.com/500-classic-family-computer-tv-game-ejc/PLID90448265")
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[@data-ref='buybox-price-main']"))).get_attribute("innerHTML"))
    
  • Console Output:

    R 345
    
  • 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


References

Link to useful documentation:

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

You are trying to get a price from buybox-module_price_2YUFa but price is actually inside the child span

//span[@data-ref='buybox-price-main']

Use following xpath to get the price

Zakaria Shahed
  • 2,589
  • 6
  • 23
  • 52