Python Forum

Full Version: How to make elements return sorted?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want my elements to be sorted in a particular manner.
laiks = driver.find_elements_by_class_name("event-date-time__time")
time.sleep(3)
speles = driver.find_elements_by_class_name("event-block-row__name")
time.sleep(4)
odds = driver.find_elements_by_class_name("odd__value")
Lets assume that by running this we get 30 different 'laiks', 30 different 'speles' and 30 different 'odds' elements. I want it to be sorted the following way: laiks1 - speles1 - odds1, laiks2 - speles2 - odds2, .... How can I do this? I have found some clues, but after looking for a while nothing definitive.
You can sort each of the lists (if that is necessary), then print them out.

In fact, if it is necessary to sort the lists, I would do so when they are assigned:
laiks = sorted(driver.find_elements_by_class_name("event-date-time__time"))
time.sleep(3)
speles = sorted(driver.find_elements_by_class_name("event-block-row__name"))
time.sleep(4)
odds = sorted(driver.find_elements_by_class_name("odd__value"))
Then print them out:

for laik, spele, odd in zip(laiks, speles, odds):
    print(f"{laik} - {spele} - {odd}")
Now I get this error on line 14:
TypeError: '<' not supported between instances of 'WebElement' and 'WebElement'
And this is the code I am running
from selenium import webdriver
import time
import pandas as pd
import json

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://www.optibet.lv/sport/wcg/CS:GO-5541")
driver.implicitly_wait(12)

#Finding els and switching to iframe
driver.switch_to.frame(driver.find_element_by_css_selector("#iFrameResizer0"))

laiks = sorted(driver.find_elements_by_class_name("event-date-time__time"))
time.sleep(3)
speles = sorted(driver.find_elements_by_class_name("event-block-row__name"))
time.sleep(4)
odds = sorted(driver.find_elements_by_class_name("odd__value"))

#print found elements
for laik, spele, odd in zip(laiks, speles, odds):
    print(f"{laik} - {spele} - {odd}")
This says that you can't (directly) sort "webelement" objects. Perhaps you want to pull some data from within the object (like text or such)? If so, you could sort the extracted data. Or, you could give sorted() a key to where the data to examine is, and it would sort the objects.

I'm not familiar with selenium, so I don't know the easy way to pull the data out, but I suspect that is the way forward.
Not a selenium user and maybe I miss something but as I understand:

- column data is acquired and sorted
- then three separately sorted columns put together using zip

This means that printed row is not row from table. It depends on sorting keyfunc but the first row with defaults will be printed as 'smallest' time, 'smallest' event name and 'smallest' odds. With high probability majority of rows constructed this way will not match actual table rows.