Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to collect name and price from things in ebay. For example I searched "armani", than I need to collect every item on first page using Selenium and append it to the list. But I have problem in my code:

mylist = list()
for a in driver.find_element_by_xpath(".//*[@id='item3aeba8d9f0']/div"):
    name = a.findElement(By.xpath(".//*[@id='item3aeba8d9f0']/div/div[2]/h3]")).getText()
    price = a.findElement(By.xpath(".//*[@id='item3aeba8d9f0']/div/div[3]/div[2]/div/span[1]/span")).getText()
    mylist.append(name, price)
print(name)

The result should like: [[name, price],[name, price],...]

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
166 views
Welcome To Ask or Share your Answers For Others

1 Answer

It sounds like you want to append tuples to the list of the form (name, price). Also you are attempting to iterate through a single web element when you want to iterate through a collection of web elements.

mylist = list()
for a in driver.find_elements_by_xpath(".//*[@id='item3aeba8d9f0']/div"): ##Change to .find_elements from .find_element
    name = a.findElement(By.xpath(".//*[@id='item3aeba8d9f0']/div/div[2]/h3]")).getText()
    price = a.findElement(By.xpath(".//*[@id='item3aeba8d9f0']/div/div[3]/div[2]/div/span[1]/span")).getText()
    mylist.append((name, price)) // THE CHANGE IS HERE!!!
    ##or you instead of tuples you could do a two element list.
    mylist.append([name, price]) // THE CHANGE IS HERE!!!
print(name)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...