1

The code clicks on a element that may or may not exist on the page and then needs to click on all elements of the same class:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.firefox.options import Options
import time

my_user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36'

options = Options()
options.set_preference("general.useragent.override", my_user_agent)
options.page_load_strategy = 'eager'
options.add_argument('--headless')
driver = webdriver.Firefox(options=options)

driver.get("https://int.soccerway.com/matches/2022/07/23/")

try:
    WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.XPATH, "//a[contains(@class,'tbl-read-more-btn')]")))
    driver.find_element(by=By.XPATH, value="//a[contains(@class,'tbl-read-more-btn')]").click()
    time.sleep(0.1)
except:
    pass
WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.XPATH, "//tr[contains(@class,'group-head')]")))
for btn in driver.find_elements(by=By.XPATH, value="//tr[contains(@class,'group-head')]"):
    btn.click()
    time.sleep(0.1)

But this work takes 90 seconds to do and when I remove the time.sleep it drops to 65 seconds, but if I remove it I notice that in some very random times some of the elements that should be clicked are ignored.

Is there any way to do this same service but clicking all the elements at the same time to speed up the process?

Buttons to click on visual examples:

enter image description here

Expected Result after clicks (open the boxes):

enter image description here

Digital Farmer
  • 1,705
  • 5
  • 17
  • 67
  • First click on `(By.XPATH, "//tr[contains(@class,'group-head')]")` takes you to the Summary Page, how do you find the elements there? – undetected Selenium Jul 25 '22 at 18:55
  • Hi @undetectedSelenium Perhaps you are being redirected to a page other than ```int.``` (for some reason) because on mine, the buttons are for clicking on the leagues I just added in the question for better viewing. – Digital Farmer Jul 25 '22 at 19:10
  • Every click is making a network call (GET) with some parametres, and retrieving some info which it paints back into the DOM. Speed is dependent of your internet network conditions, as well as of server performance. – Barry the Platipus Jul 25 '22 at 20:45
  • Hi @platipus_on_fire_333 I understand, but in this case, it's a very good internet and my own computer, so there's no problem with the quality itself. The problem really is the time it takes to click on all of them one by one, so my question about there is a faster way to get to the same result (for example, if there is a way to click on them all at once) . – Digital Farmer Jul 25 '22 at 21:14
  • You forgot about *server performance* variable, in this context. Anyway, I just tested it, and had to give it longer wait times (0.5) to have all clicks register reliably. Given the conditions, I guess your timings are fine. – Barry the Platipus Jul 25 '22 at 21:44

1 Answers1

1

In order to speed up the process you can click on all the competition-link one by one in a sequence using either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    for ele in driver.find_elements(By.CSS_SELECTOR, "tr.group-head.clickable th.competition-link>a"):
        ele.click()
    
  • Using XPATH:

    for ele in driver.find_elements(By.XPATH, "//tr[@class='group-head  clickable ']//th[@class='competition-link']/a"):
        ele.click()
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Hi, thanks for your time to help me, but I'm actually really trying to click on the element so that it opens and shows the hidden games that each of them has. I updated the question adding the expected result more clearly. – Digital Farmer Jul 25 '22 at 21:10