【发布时间】:2022-01-21 01:05:54
【问题描述】:
这是我目前写的代码:
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()
【问题讨论】:
-
那么
text属性必须为空。 -
还是没听懂,什么意思?
-
你没有说程序抛出异常,所以
print()一定是在执行。如果您没有看到任何输出,这一定意味着find_element_by_xpath()找到了一个带有空白文本的元素。所以你的 xpath 可能是错误的。 -
当你手动找到那个元素时,上面的文字是什么?您可能正在寻找 value 属性或类似的东西。
-
请查看编辑,xpath 指向它,我检查了。
标签: python selenium web-scraping