D
D
Dmitriy_pythonshik2020-06-21 16:39:38
Python
Dmitriy_pythonshik, 2020-06-21 16:39:38

How to click on several buttons with different classes on a website?

Good afternoon everyone. There is a python parser that clicks on buttons, but there are 4 types of buttons on the site and they are scattered on the site and they have different classes. Tell me how you can specify not 1 class, but all 4, and so that they are clicked in the cycle?

Here is the code, but it skips clicking only on arr, it turns out that it skips arr1, arr2, arr3:

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())
count = 0

driver = webdriver.Chrome(executable_path="C:\\Users\\user\\PycharmProjects\\parser_proj\\chromedriver") # здесь укажите путь до хром драйвера, он обычно в той же папке где и сам файл с кодом
driver.get('https://www.flashscore.ru/')
time.sleep(3)

arr = driver.find_elements_by_css_selector(".event__match.event__match--scheduled.event__match--oneLine.event__match.event__match--live.event__match--last.event__match--oneLine")
arr1 = driver.find_elements_by_css_selector(".event__match.event__match--last.event__match--oneLine")
arr2 = driver.find_elements_by_css_selector(".event__match.event__match--last.event__match--oneLine")
arr3 = driver.find_elements_by_css_selector(".event__match.event__match--live.event__match--last.event__match--oneLine")

driver.set_page_load_timeout(5)

for channel in arr + arr1 + arr2 + arr3:
    try:
        channel.click()
        count += 1
        if count == 16:
            driver.execute_script("window.scrollTo(0, 600);")
            time.sleep(3)

        elif count == 28:
            driver.execute_script("window.scrollTo(600, 1100);")
            time.sleep(3)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Elvis, 2020-06-21
@Dr_Elvis

and if you iterate over the selectors:

selectors = ['.event__match.event__match--scheduled.event__match--oneLine.event__match.event__match--live.event__match--last.event__match--oneLine', '.event__match.event__match--last.event__match--oneLine', '.event__match.event__match--last.event__match--oneLine', '.event__match.event__match--live.event__match--last.event__match--oneLine']

for selector in selectors:
    arr = driver.find_elements_by_css_selector(selector)
    for channel in arr:
        try:
            channel.click()
            count += 1
            if count == 16:
                driver.execute_script("window.scrollTo(0, 600);")
                time.sleep(3)

            elif count == 28:
                driver.execute_script("window.scrollTo(600, 1100);")
                time.sleep(3)

By the way, the second and third selectors are the same, maybe this is a mistake.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question