Selenium webdriver Python | TypeError: 'str' object is not callable

Selenium webdriver Python | TypeError: 'str' object is not callable

我想从网格中获取 Phone 名称、描述、价格。

但是出现以下错误:

元素=id.find_elements(By.XPATH("//div[包含(@class,'ProductModule__imageAndDescriptionWrapper')]")) 类型错误:'str' 对象不可调用

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome(r"chromedriver.exe")

driver.get("https://www.tatacliq.com/apple/c-mbh12e00008")

id = driver.find_element_by_id('grid-container')

element = id.find_elements(By.XPATH("//div[contains(@class,'ProductModule__imageAndDescriptionWrapper')]"))

print(element)

for i in element:
    print(i.get_attribute('a'))

我猜应该是

element = id.find_elements(By.XPATH, "//div[contains(@class,'ProductModule__imageAndDescriptionWrapper')]")

'a' 不是属性而是标签。所以你必须调用 href as attribute

element = id.find_elements(By.XPATH("//div[contains(@class,'ProductModule__imageAndDescriptionWrapper')]"))

print(element)

for i in element:
    print(i.a.get_attribute('href'))


#OR



element = id.find_elements(By.XPATH("//div[contains(@class,'ProductModule__imageAndDescriptionWrapper')]/a"))

print(element)

for i in element:
    print(i.get_attribute('href'))