Skip to content
Advertisement

How to filter Selenium results setting a query limit?

I managed to get the data I wanted with selenium, but now I only need the first 17 data that it gives me, I need to make a kind of filter with this data, because I’m going to use conditions on top of them to use in another code.

from ctypes.wintypes import PINT
from logging import root
from tkinter import N
from hyperlink import URL
from numpy import number
from selenium import webdriver
import selenium
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.expected_conditions import presence_of_element_located
import time

url = "https://blaze.com/pt/games/double"

#absolute path
firefox_driver_path = "/Users/Antônio/Desktop/roletarobo/geckodriver.exe"
firefox_options = Options()
firefox_options.add_argument("--headless")

webdriver = webdriver.Firefox(
    executable_path = firefox_driver_path,
    options = firefox_options
)

with webdriver as driver:
    # timeout
    wait = WebDriverWait(driver, 1)

    # retrieve data
    driver.get(url)

    #wait
    wait.until(presence_of_element_located((By.ID, "roulette-recent")))

    results = driver.find_elements(by=By.CLASS_NAME, value='entry')#find_elements_by_css_selector('#roulette .sm-box .number')
    for quote in results:
      quoteArr = quote.text.split('n')
      print(quoteArr)
    

    driver.close()

    

My result is below

['']
['13']
['4']
['11']
['11']
['13']
['6']
['5']
['9']
['14']
['5']
['']
['12']
['10']
['5']
['']
['3']
['13']
['']
['']
['Douglas', 'R$ 39.48']
['MSK', 'R$ 25.27']
['Marcz10', 'R$ 23.69']
['Jeferson ☘️', 'R$ 19.74']
['Pai_daBlaze', 'R$ 12.35']
['Lucianotelles1993', 'R$ 11.84']
['Alexandra souza', 'R$ 11.84']
['Taynara Luna', 'R$ 9.87']
['Mylla coutinho', 'R$ 9.87']
['Marcos smith', 'R$ 9.87']

As you can see he gave me several returns but I only need the first 17 from top to bottom in order, I don’t need these names below nor this information that is in front of them. How can I do that?

Advertisement

Answer

To extract the first 17 datas you can use List Slicing and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    driver.get("https://blaze.com/pt/games/double")
    print([my_elem.text for my_elem in driver.find_elements(By.CSS_SELECTOR, "div#roulette-recent div.entry")][:17])
    
  • Using XPATH:

    driver.get("https://blaze.com/pt/games/double")
    print([my_elem.text for my_elem in driver.find_elements(By.XPATH, "//div[@id='roulette-recent']//div[@class='entry']")][:17])
    
  • Console Output:

    ['13', '4', '10', '3', '2', '10', '5', '14', '5', '6', '8', '4', '5', '8', '4', '7', '1']
    
Advertisement