Issue
im practically a fetus when it comes to python programming and i hope some of you gods would be able to help me. basically i have a loop set up that pulls values from a website but i need it run indefinitely until i press the specified hotkey which is q, the loop runs and pulls the data needed, but whenever i press q, the loop simply keeps running and i have to close the browser manually.
here is the 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.support import expected_conditions as ec
from selenium.webdriver.common.by import By
import time
import keyboard
PATH = r"C:\Users\username"
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()
if keyboard.is_pressed('q'):
kill_chromedriver()
break
print('done reading')
so how do i edit this code, so that the driver closes and the loop stops on command? ive tried positioning the break statement in different places, and also placing the cweb.close() function out of the loops. ill continue running through other options. i would be extremely grateful for any tips or help.
edits: took some help from an extremely useful lad, but it seems as if pressing the hotkey is recognized by the computer and python, but not recognized by selenium or the browser session itself. wish i knew more about code like yall do lmao
G-man
Solution
I've found .quit() to fail sometimes with selenium. You can use psutil instead. Call this function in the if keyboard.is_pressed('q'):
block.
import psutil
def kill_chromedriver():
for proc in psutil.process_iter():
if proc.name() == "chromedriver.exe":
proc.kill()
Edit: I think your issue might be related to read_key() not executing until the data are pulled. I tested this on my cpu in Jupyter and it worked.
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
Answered By - Matthew Borish
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.