1

The driver is set to be headless and there is a loop that iterates through URLs and gets some action done. The idea is to run this in the background without disrupting workflow but the terminal keeps popping up with this code. If I would execute the program in the terminal, it is fine because all the output is sent in one place but there is a need for a GUI. I tried moving the driver so that it would be share across the URLs fetched but there is an ID error and appears that a new driver instance needs to be created. There is multiprocessing to avoid Tkinter from freezing by running multiple processes. Pyinstaller was also used to execute the program. Using the command "pyinstaller -w -F --onefile TMO_Scrub.pyw" but still not luck. Seems its the console from the chrome driver.

Following is the code:

import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from tkinter import *
import multiprocessing as mp


class Scrub:
    def __init__(self, master):
        mp.freeze_support()
        self.master = master

        self.submit_btn = Button(
            self.master, text='Submit', command=self.submit)
        self.submit_btn.pack()

    def submit(self):
        self.t1 = mp.Process(target=execute)
        self.t1.daemon = True
        self.t1.start()


def execute():
    try:
        urls = ['https://www.google.com/', 'https://www.youtube.com/',
                'https://www.instagram.com/', 'https://www.facebook.com/', 'https://www.cnn.com/']
        for url in urls:
            scrape_page(url)
    except NameError:
        print('There was an error...')


def scrape_page(url):
    opts = Options()
    opts.add_argument("headless")
    chrome_driver = os.getcwd() + "\chromedriver.exe"
    driver = webdriver.Chrome(options=opts, executable_path=chrome_driver)
    driver.get(url)
    driver.quit()


if __name__ == '__main__':
    root = Tk()
    gui = Scrub(root)
    root.mainloop()

Axisnix
  • 2,822
  • 5
  • 19
  • 41
hyl3rid
  • 23
  • 1
  • 5
  • What exactly is the problem? Is the chrome driver's console getting focus? – The Amateur Coder Oct 02 '21 at 09:41
  • That is correct, the terminal for chromedriver keeps getting focus. It closes and opens as the web pages get scraped. – hyl3rid Oct 03 '21 at 13:26
  • You can check if the Tkinter window has focus, using `window.focus_displayof()`, and then do the scraping work. This might help: https://stackoverflow.com/questions/62613379/how-to-detect-if-my-tkinter-windows-has-focus. If the window isn't in focus, you can request focus using the win32gui library: `win32gui.SetForegroundWindow(root.winfo_id())` as in https://stackoverflow.com/questions/22751100/tkinter-main-window-focus. Also, if you want to set the focus on any other window; you can get it's PID, and then set focus with `win32gui.SetForegroundWindow()` Hope that solves the problem :) – The Amateur Coder Oct 04 '21 at 01:50

0 Answers0