Issue
I am trying to append car links from a website into a list. I want to traverse that list to get information from each of the car's web pages.
So far I have tried both .append method as well as += operator method but I get the same errors for both which is :
AttributeError: 'str' object has no attribute 'get_attribute'
This only shows up when I use the following line of code:
carLinks += [carLink.get_attribute("href")]
or the append
method. However, if I just print the carLink.get_attribute("href")
then it prints all the links.
This is the partial code I used:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://www.tred.com/buy?body_style=&distance=50&exterior_color_id=&make=&miles_max=100000&miles_min=0&model=&page_size=24&price_max=100000&price_min=0&query=&requestingPage=buy&sort=desc&sort_field=updated&status=active&year_end=2022&year_start=1998&zip=")
carLinks = []
carLinks = driver.find_elements_by_css_selector("div.grid-box-container a")
for carLink in carLinks:
carLinkUrl = carLink.get_attribute("href")
carLinks.append(carLinkUrl)
# print(carLinkUrl)
print(carLinks)
driver.quit()
I haven't tried it in BeautifulSoup yet as I am not used to mixing both Selenium and BeautifulSoup at once.
Solution
This is cause you have a list of and name is carLinks
. Also in your loop :
for carLink in carLinks:
carLinkUrl = carLink.get_attribute("href")
carLinks.append(carLinkUrl)
You have same name of a web element.
Compiler will think carLinks
is an web element
because of local scope.
and since carLinks
is locally a web element
, there is no append
method available in Selenium.
Please change either one of names.
carLinks = []
links = driver.find_elements_by_css_selector("div.grid-box-container a")
for car_link in links:
carLinks.append(car_link.get_attribute('href'))
print(carLinks)
Answered By - cruisepandey
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.