'(Python)Selenium Webdriver while loop doesnt restart after if statement. Any ideas why?

I'm a disabled decrepit fetus when it comes to python programming lmao, id like to get some more help and knowledge from more advanced practitioners. i have a small program that's supposed to pull values from a website. i have it set up so its supposed to run indefinitely, through a collection of websites, pulling these values until i press a specified hotkey. the loop runs through and executes the creation of the classes and the relating functions but refuses to restart the loop after a certain if statement. id be extremely grateful for any help

piece of code in question:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
import time
import keyboard
import psutil

PATH = r"C:\Users\Grayson"
options = Options()
options.binary_location = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
ser = Service(executable_path=ChromeDriverManager().install())
cweb = webdriver.Chrome(service=ser, options=options)

class Coin:
   def __init__(self, coinname, datasource):
        self.coinname = coinname
        self.datasource = datasource
   
   def pulldata(self):
        self.values = []
        time.sleep(5)
        cweb.get(self.datasource)
        time.sleep(5)
 
        price = cweb.find_element(By.XPATH, '//*[@id="__next"]/div[1]/div[1]/div[2]/div/div[1]/div[2]/div/div[2]/div[1]/div/span')
        percent = cweb.find_element(By.XPATH, '//*[@id="__next"]/div[1]/div[1]/div[2]/div/div[1]/div[2]/div/div[2]/div[1]/span')
        upordown = cweb.find_element(By.XPATH, '//*[@id="__next"]/div[1]/div[1]/div[2]/div/div[1]/div[2]/div/div[2]/div[1]/span/span')
        caret = upordown.get_attribute('class')

        if caret == 'icon-Caret-up':
           daychange = ('' + percent.text)
        
        elif caret == 'icon-Caret-down':
           daychange2 = ('-' + percent.text)


        vol = cweb.find_element(By.XPATH, '//*[@id="__next"]/div[1]/div[1]/div[2]/div/div[1]/div[2]/div/div[3]/div[1]/div[3]/div[1]/div[2]/div')
        mkcap = cweb.find_element(By.XPATH, '//*[@id="__next"]/div[1]/div[1]/div[2]/div/div[1]/div[2]/div/div[3]/div[1]/div[1]/div/div[2]/div')
        circsupply = cweb.find_element(By.XPATH, '//*[@id="__next"]/div[1]/div[1]/div[2]/div/div[1]/div[2]/div/div[3]/div[1]/div[4]/div[2]/div[1]')
        self.values.append(price.text) 

        if caret == 'icon-Caret-up':
           self.values.append(daychange)

        elif caret == 'icon-Caret-down':
           self.values.append(daychange2)

        self.values.append(vol.text)
        self.values.append(mkcap.text)
        self.values.append(circsupply.text)
        print(self.values)

def kill_chromedriver():   
    for proc in psutil.process_iter():
       if proc.name() == "chromedriver.exe":
           proc.kill()
       if proc.name() == "chrome.exe":
           proc.kill()

while True:
   btc = Coin('Bitcoin','https://coinmarketcap.com/currencies/bitcoin/')
   btc.pulldata()
   btc2 = Coin('Bitcoin','https://coinmarketcap.com/currencies/tether/')
   btc2.pulldata()

   print("You can press q now.")
   if keyboard.read_key() == "q":
   
       kill_chromedriver()
       break

print('done reading')

i tried:

  • positioning the if statement at the beginning of the loop, but that just makes the loop freeze up before it executes the first two functions
  • using the .is_pressed() function instead of the .read_key() function in the keyboard modules, but that just results in the loop continuing indefinitely, unable to recognize whenever i press the hotkey and refusing to close after i do so.

as said before i would be extremely grateful for anyone willing to go out of their way to help me. ill be trying other things and working on other stuff in the meantime.

G-Man



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source