Python Forum
How to make elements return sorted?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to make elements return sorted?
#1
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.
Reply
#2
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}")
Reply
#3
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}")
Reply
#4
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.
Reply
#5
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Failing to print sorted files tester_V 4 1,188 Nov-12-2022, 06:49 PM
Last Post: tester_V
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,013 May-17-2022, 11:38 AM
Last Post: Larz60+
  set and sorted, not working how expected! wtr 2 1,254 Jan-07-2022, 04:53 PM
Last Post: bowlofred
  Make Groups with the List Elements quest 2 1,934 Jul-11-2021, 09:58 AM
Last Post: perfringo
  Sorting Elements via parameters pointing to those elements. rpalmer 3 2,549 Feb-10-2021, 04:53 PM
Last Post: rpalmer
  Why is my original list also sorted? Pedroski55 1 1,581 Jul-15-2020, 09:25 PM
Last Post: Yoriz
  Outputting Sorted Text files Help charlieroberrts 1 1,684 Jul-05-2020, 08:37 PM
Last Post: menator01
  sorted function example mystery sabaidii2 4 2,482 Feb-10-2020, 09:37 AM
Last Post: DeaD_EyE
  Byte array is sorted when sending via USB daviddlc68 1 2,776 Aug-16-2019, 10:11 AM
Last Post: wavic
  sorted object in list trois 2 2,209 Mar-04-2019, 09:12 AM
Last Post: trois

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020