S
S
Snake2021-09-02 17:39:25
Python
Snake, 2021-09-02 17:39:25

Python. Tkinter + Selenium - function output via radio button not working?

# Графическая часть
from tkinter import *
from selenium import webdriver
driver = webdriver.Chrome('Путь к chromedriver.exe')

root = Tk()
root.title("YouTube Search")
root.geometry("500x500")


def show_message():
    driver.get('https://www.youtube.com/')  # ссылка сайта, на котором будет выполняться действие
    search = driver.find_element_by_xpath('//*[@id="search"]')  # строка ввода
    search.send_keys(detect.get())  # ввод содержания
    button = driver.find_element_by_xpath('//*[@id="search-icon-legacy"]/yt-icon')  # поиск > наведение
    button.click()  # поиск > клик


def watch_filtr2():
    icon = driver.find_element_by_xpath('/html/body/ytd-app/div/ytd-page-manager/ytd-search/div[1]/ytd-two-column-search-results-renderer/div/ytd-section-list-renderer/div[1]/div[2]/ytd-search-sub-menu-renderer/div[1]/div/ytd-toggle-button-renderer/a/tp-yt-paper-button/yt-formatted-string')
    icon.click()
    filtr2 = driver.find_element_by_xpath('/html/body/ytd-app/div/ytd-page-manager/ytd-search/div[1]/ytd-two-column-search-results-renderer/div/ytd-section-list-renderer/div[1]/div[2]/ytd-search-sub-menu-renderer/div[1]/iron-collapse/div/ytd-search-filter-group-renderer[5]/ytd-search-filter-renderer[2]/a/div/yt-formatted-string')
    filtr2.click()


def watch_filtr3():
    icon = driver.find_element_by_xpath('/html/body/ytd-app/div/ytd-page-manager/ytd-search/div[1]/ytd-two-column-search-results-renderer/div/ytd-section-list-renderer/div[1]/div[2]/ytd-search-sub-menu-renderer/div[1]/div/ytd-toggle-button-renderer/a/tp-yt-paper-button/yt-icon')
    icon.click()
    filtr3 = driver.find_element_by_xpath('/html/body/ytd-app/div/ytd-page-manager/ytd-search/div[1]/ytd-two-column-search-results-renderer/div/ytd-section-list-renderer/div[1]/div[2]/ytd-search-sub-menu-renderer/div[1]/iron-collapse/div/ytd-search-filter-group-renderer[5]/ytd-search-filter-renderer[3]/a/div/yt-formatted-string')
    filtr3.click()


def watch_filtr4():
    icon = driver.find_element_by_xpath('/html/body/ytd-app/div/ytd-page-manager/ytd-search/div[1]/ytd-two-column-search-results-renderer/div/ytd-section-list-renderer/div[1]/div[2]/ytd-search-sub-menu-renderer/div[1]/div/ytd-toggle-button-renderer/a/tp-yt-paper-button/yt-icon')
    icon.click()
    filtr4 = driver.find_element_by_xpath('/html/body/ytd-app/div/ytd-page-manager/ytd-search/div[1]/ytd-two-column-search-results-renderer/div/ytd-section-list-renderer/div[1]/div[2]/ytd-search-sub-menu-renderer/div[1]/iron-collapse/div/ytd-search-filter-group-renderer[5]/ytd-search-filter-renderer[4]/a/div/yt-formatted-string')
    filtr4.click()


# Комментарий для пользователя
label = Label(root, text="↓ Ввод запроса ↓", fg='white', bg="#DC143C", pady=10, padx=1920, cursor="sb_down_arrow")
label.pack()

# Ввод запроса
detect = Entry(root, width=1920, bg='#A9A9A9', justify='center', font=('Arial', 12))
detect.pack()


filtr_var = IntVar()
Radiobutton(text='По релевантности', variable=filtr_var, value=1, activebackground='Gold', command='start').pack(anchor=W)
Radiobutton(text='По дате загрузки', variable=filtr_var, value=2, activebackground='Gold', command='watch_filtr2').pack(anchor=W)
Radiobutton(text='По числу просмотров', variable=filtr_var, value=3, activebackground='Gold', command='watch_filtr3').pack(anchor=W)
Radiobutton(text='По рейтингу', variable=filtr_var, value=4, activebackground='Gold', command='watch_filtr4').pack(anchor=W)


# Кнопка
button = Button(root, text="Поиск", fg="white", bg="#DC143C", activebackground='pink',
                command=show_message, pady=10, padx=1920)
button.pack()

root.mainloop()


Hello, I am new to this topic. The program should filter the video request for YouTube, but I can’t understand: there is a function, it is written through the command in the buttons, but there is no output.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-09-02
@Ne1man

> command='watch_filtr2'
> ​​command=show_message
can you see the difference? The first does not work for you, the second, as I understand it, works.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question