Home > Enterprise >  Text is not printed when using selenium
Text is not printed when using selenium

Time:12-19

This is the code I have written so far:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException

chrome_driver_path = "C:\Development\chromedriver.exe"
driver = webdriver.Chrome(executable_path=chrome_driver_path)

driver.get("https://www.amazon.ae/Kingston-SA400S37-480G-480GB-A400/dp/B01N6JQS8C/ref=lp_12050241031_1_12?th=1")
price = driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[7]/div[3]/div[4]/div[11]/div[1]/div/table/tbody/tr[2]/td[2]/span[1]/span[1]').text
print(price)



#driver.close()
driver.quit()

This doesn't print out the price, please help. This is what the output terminal looks like. output terminal

I want to get this price: price

CodePudding user response:

The value of the price is blank.

You should replace the tailing span[1] with span[2] in your xpath

Here is the code -

from selenium import webdriver


chrome_driver_path = "drivers\\chromedriver.exe"
driver = webdriver.Chrome(executable_path=chrome_driver_path)

driver.get("https://www.amazon.ae/Kingston-SA400S37-480G-480GB-A400/dp/B01N6JQS8C/ref=lp_12050241031_1_12?th=1")
price = driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[7]/div[3]/div[4]/div[11]/div[1]/div/table/tbody/tr[2]/td[2]/span[1]/span[2]').text
print("price: ", price)

driver.close()
driver.quit()

Output -

price:  AED81.64
  • Related