Hide console by webdriver in windows PyQT5 application
Asked Answered
B

0

2

This question is absolutely the same as this. But solutions there are not working for me :(

I'm using undetected-chromedriver, webdriver-manager and trying to put selenium scrapper behind the scene of PyQT5 app which i build with pyinstaller


  1. First i tried this answer by @Ali Sajjad. Trying set self.creationflags = CREATE_NO_WINDOW in Service object. I added prints to see changes.
import undetected_chromedriver.v2 as uc
from selenium.webdriver.common.service import Service
from webdriver_manager.chrome import ChromeDriverManager


class Scrapper:
    def __init__(self):
        self.driver = None
        self.page_load_time_out = 20

    def get_driver(self):
        # options = webdriver.ChromeOptions()
        options = uc.ChromeOptions()
        ...
        chrome_service = Service(ChromeDriverManager().install())
        # try except block because import raising error not on windows machines
        print(chrome_service.creationflags)
        try:
            from subprocess import CREATE_NO_WINDOW
            chrome_service.creationflags = CREATE_NO_WINDOW
        except ImportError:
            pass
        print(chrome_service.creationflags)
        driver = uc.Chrome(
            options=options,
            service=chrome_service
        )
        self.driver = driver
        print(self.driver.service.creationflags)

    def get_page(self, url):
        self.get_driver()
        print(self.driver.service.creationflags)
        self.driver.set_page_load_timeout(self.page_load_time_out)
        self.driver.get(url)
        ...

1) Console output:

0
134217728
0
0
  1. Next step by me setting self.driver.service.creationflags = CREATE_NO_WINDOW. Added # difference.
import undetected_chromedriver.v2 as uc
from selenium.webdriver.common.service import Service
from webdriver_manager.chrome import ChromeDriverManager


class Scrapper:
    def __init__(self):
        self.driver = None
        self.page_load_time_out = 20

    def get_driver(self):
        # options = webdriver.ChromeOptions()
        options = uc.ChromeOptions()
        ...
        chrome_service = Service(ChromeDriverManager().install())
        # try except block because import raising error not on windows machines
        print(chrome_service.creationflags)
        try:
            from subprocess import CREATE_NO_WINDOW
            chrome_service.creationflags = CREATE_NO_WINDOW
        except ImportError:
            pass
        print(chrome_service.creationflags)
        driver = uc.Chrome(
            options=options,
            service=chrome_service
        )
        self.driver = driver
        # difference !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        try:
            from subprocess import CREATE_NO_WINDOW
            self.driver.service.creationflags = CREATE_NO_WINDOW
        except ImportError:
            pass
        print(self.driver.service.creationflags)

    def get_page(self, url):
        self.get_driver()
        print(self.driver.service.creationflags)
        self.driver.set_page_load_timeout(self.page_load_time_out)
        self.driver.get(url)
        ...

2) Console output:

0
134217728
134217728
134217728

So in my opinion it should works (cmd window of undetected-chromedriver should not open) now because value of creationflags changed, BUT IT'S NOT WORKING... still see this: enter image description here

  1. Then i tried this solution from same question...Just did the same thing like in soulution in source code of selenium.webdriver.common.service and It's working but only on mine machine. If i share my app.exe, other people can't even run the script. They press button and app become frozen. But on my PC it's working, i don't see console by undetected-chromedriver.

Any help please. I'm building this app on virtual box's windows, it's painful enough if do this too long...

Brio answered 24/9, 2022 at 21:14 Comment(6)
Why are you ignoring ImportError here? The CREATE_NO_WINDOW flag is definitely available in windows.Renounce
@AliSajjad I'm using linux on my pc and building this app on virtual machine. If i use this import on linux, i get ImportError. Anyway it doesn't matter because it is constant and i can see that i'm getting it with prints. Thanks for replyBrio
It depends on the selenium version you are using. Try 4.5.0Socialite
Did you got to fix it?Amharic
@farhanjatt nope. May be Squalo provided solution, but i didn't tryBrio
The link below can help you completely, it worked for me https://mcmap.net/q/831371/-hide-command-prompt-in-selenium-chromedriverLysin

© 2022 - 2024 — McMap. All rights reserved.