pkk
pkk

Reputation: 379

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

I wanted to get the Phone name ,description, price from the grid.

However getting the below error:

element = id.find_elements(By.XPATH("//div[contains(@class,'ProductModule__imageAndDescriptionWrapper')]")) TypeError: 'str' object is not callable

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'))

enter image description here

Upvotes: 0

Views: 1766

Answers (2)

Md. Fazlul Hoque
Md. Fazlul Hoque

Reputation: 16187

'a' isn't attribute but a tag. So you have to call 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'))

Upvotes: 1

sudden_appearance
sudden_appearance

Reputation: 2195

I guess it should be

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

Upvotes: 1

Related Questions