I am trying to create this app so that it finds the streamers views, if it doesn't find the XPATH I want it to output "Streamer is offline" but it just outputs "Streamer is offline" even for online streamers. Also, when typing a new streamer the label does not overwrite, rather creates a new line.
from tkinter import *
import tkinter.font as font
from functools import partial
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
# Gets the webpage "Twitch" then finds the user inputted and eventually displays the viewer count.
def string_twitch(twitchname):
PATH = "/home/networkmojito/python/Tkinter/chromedriver"
options = webdriver.ChromeOptions()
options.add_argument('headless')
options.add_argument('window-size=1920x1080')
options.add_argument("disable-gpu")
driver = webdriver.Chrome(PATH, options = options)
driver.get("https://twitch.tv/" + twitchname.get())
try:
twitch_views = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "/html/body/div[1]/div/div[2]/div/main/div[2]/div[3]/div/div/div[1]/div[1]/div[2]/div/div[1]/div/div/div/div[2]/div[2]/div[2]/div/div/div[1]/div[1]/div/p"))
)
twitch_viewers = twitch_views.text()
views_amount = Label(text = "There are currently " + twitch_viewers + " Live viewers in " + twitchname.get() + "'s stream.", fg = 'white', bg = 'black', font = font_ubun_size2)
views_amount.pack()
except:
streamer_offline = Label(text = "Streamer is currently offline.", fg = 'white', bg = 'black', font = font_ubun_size2)
streamer_offline.pack()
driver.quit()
# creates the window, and "className = x" names the window
window = Tk(className = "Twitch Viewer")
# Window size
window.geometry("700x150")
window.configure(bg = "black")
font_ubun_size = font.Font(size = 28, family = 'Ubuntu Mono')
font_ubun_size2 = font.Font(size = 16, family = 'Ubuntu Mono')
# Username: and also box to enter the username, StringVar, just reveals the text as it is.
user_input = Label(text = "Enter Twitch Username:", fg = "purple", bg = "black", font = font_ubun_size)
user_input.pack()
twitchname = StringVar()
twitch_username = Entry(textvariable = twitchname, bg = '#88979e')
twitch_username.pack()
string_twitch = partial(string_twitch, twitchname)
submit_button = Button(text = "Submit.", fg = "white", bg = "black", activebackground = '#00cc70', activeforeground = 'white', command = string_twitch)
submit_button.pack()
window.mainloop()