【发布时间】:2021-11-05 05:47:03
【问题描述】:
这里是 Selenium 和 Python 的新手。我正在尝试获取商品的价格。
但我收到此错误:'str' 对象不可调用。我做错了什么?
from bs4 import BeautifulSoup
import requests
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
url = "https://www.staples.ca/products/959340-en-dr-pepper-355-ml-cans-12-pack"
s=Service('C:/Users/teddy/OneDrive/Desktop/chromedriver.exe')
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--incognito')
options.add_argument('--headless')
driver = webdriver.Chrome(service=s)
driver.get(url)
get_elements = driver.find_element(By.XPATH("//span[@data-product-id='2151359512645'']"))
# print(get_elements.text)
【问题讨论】:
-
您在 xpath 的末尾添加了一个额外的引号。应该是
//span[@data-product-id='2151359512645']。