简体   繁体   中英

Add all elements with the same class to list with selenium and python

How do I add all of the elements in a webpage to a list (array) in python with selenium? Ive tried things like:

list = driver.find_elements_by_class_name("classname")

list = driver.find_elements(by="class", "classname")

When i print the list after this, it only shows [].

You can print elements from list using the below approach:

list  = driver.find_elements_by_class_name("classname")
for item in list:
    print (item.text)

What I found out worked, was doing

list_of_elements = driver.find_element_by_xpath('//*[@class="classname"]')

Then I did

names = []
for i in range(len(list_of_elements)):
   names.append(list_of_elements[i].text)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM