Python Forum
Chromedriver launch new tab - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/Forum-Python-Coding)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/Forum-Web-Scraping-Web-Development)
+--- Thread: Chromedriver launch new tab (/Thread-Chromedriver-launch-new-tab)

Pages: 1 2


Chromedriver launch new tab - gahhon - Feb-10-2019

So I planned to make a GUI for my application which contains several buttons.
Each button will open chrome browser and redirect to specific URLs.
So this is my sample GUI code, but will enhance the code to look more perfect.

from tkinter import *
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
import time

window = Tk()
window.title("Welcome to MyApp")
window.geometry('350x200')

def google():
    chrome_options = Options()
    chrome_options.add_argument("--disable-infobars")
    chrome_options.add_argument("--headless")
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_argument('--log-level=3')

    driver = webdriver.Chrome(executable_path=r'chromedriver.exe')
    driver.get("https://www.google.com")
 
btnGoogle = Button(window, text="Google", command=google)
btnGoogle.grid(column=0, row=0)

def yahoo():
    chrome_options = Options()
    chrome_options.add_argument("--disable-infobars")
    chrome_options.add_argument("--headless")
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_argument('--log-level=3')

    driver = webdriver.Chrome(executable_path=r'chromedriver.exe')
    driver.get("https://malaysia.yahoo.com/?p=us")

btnYahoo = Button(window, text="Yahoo", command=yahoo)
btnYahoo.grid(column=1, row=0)

window.mainloop()
EDIT
I think I'll use another application to create the fancy GUI and to trigger the .py/.exe file. (1 button will trigger 1 .py/.exe file)
So there are few things I wanna do ask.
  1. How to launch default google chrome browser instead of chromedriver.exe (To avoid the pop-up window when executed chromedriver.exe)
  2. How to launch as new tab and redirect to specific URLs, if the chrome browser is already opened, else open new browser window.
  3. How to convert .py to .exe file



RE: Chromedriver launch new tab - Larz60+ - Feb-10-2019

see: https://python-forum.io/Thread-Web-scraping-part-2
search for chromedriver


RE: Chromedriver launch new tab - metulburr - Feb-10-2019

I dont believe you can use selenium without using the driver. What popup window are you referring to? A popup can be dismissed via selenium. driver.switch_to can handle switching windows such as popups or tabs. In which you would switch to it and close it, then return to the parent window. You can also try to dismiss notifications before via something similar to this depending on what popup you are talking about
    def chrome_prep(self):
        '''get rid of asking to save password and notifications popup'''
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_experimental_option(
            'prefs', {
                'credentials_enable_service': False,
                "profile.default_content_setting_values.notifications" : 2,
                'profile': {
                    'password_manager_enabled': False
                }
            }
        )
        return chrome_options
         
    def setup_chrome(self):
        options = self.chrome_prep()
        self.browser = webdriver.Chrome(CHROMEPATH, chrome_options=options)
You can open a new tab via javascript driver.execute_script("window.open('');") and then switch to it and open a url. Such as:
    #driver.get("https://malaysia.yahoo.com/?p=us")
    driver.execute_script("window.open('');")
    driver.switch_to.window(driver.window_handles[1])
    driver.get('https://python-forum.io')
    driver.switch_to.window(driver.window_handles[0])
    driver.get('https://malaysia.yahoo.com/?p=us')
You can build an exe using Pyinstaller


RE: Chromedriver launch new tab - gahhon - Feb-11-2019

(Feb-10-2019, 06:39 PM)metulburr Wrote: I dont believe you can use selenium without using the driver. What popup window are you referring to? A popup can be dismissed via selenium. driver.switch_to can handle switching windows such as popups or tabs. In which you would switch to it and close it, then return to the parent window. You can also try to dismiss notifications before via something similar to this depending on what popup you are talking about
    def chrome_prep(self):
        '''get rid of asking to save password and notifications popup'''
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_experimental_option(
            'prefs', {
                'credentials_enable_service': False,
                "profile.default_content_setting_values.notifications" : 2,
                'profile': {
                    'password_manager_enabled': False
                }
            }
        )
        return chrome_options
         
    def setup_chrome(self):
        options = self.chrome_prep()
        self.browser = webdriver.Chrome(CHROMEPATH, chrome_options=options)
You can open a new tab via javascript driver.execute_script("window.open('');") and then switch to it and open a url. Such as:
    #driver.get("https://malaysia.yahoo.com/?p=us")
    driver.execute_script("window.open('');")
    driver.switch_to.window(driver.window_handles[1])
    driver.get('https://python-forum.io')
    driver.switch_to.window(driver.window_handles[0])
    driver.get('https://malaysia.yahoo.com/?p=us')
You can build an exe using Pyinstaller

Whenever I launch the chromedriver.exe it will popped 1 new window, and I don't want it be showed.
Capture


Regarding the chrome_options, I don't know why I can't execute as I did follow online example.
It said, "use options instead of chrome_options"

Regarding the tab via JavaScript, how can I check? If Chrome Broswer is open then open as new tab, else open new Chrome Browser.

Thanks.


RE: Chromedriver launch new tab - metulburr - Feb-11-2019

i dont get that window....but im in linux. Im sure there is a flag somewhere to stop it.

https://stackoverflow.com/questions/25871898/how-to-hide-chromedriver-console-window

Quote:
(Feb-11-2019, 02:55 PM)gahhon Wrote: If Chrome Broswer is open then open as new tab, else open new Chrome Browser.
It shouldnt matter. driver.get() will open the site. If you are on your current tab, it will change it, if you switch to a new tab, it will open it in that tab, and so on and so on. You are opening each and every tab, so you would know how many you open and if you need more.


RE: Chromedriver launch new tab - gahhon - Feb-13-2019

(Feb-11-2019, 09:21 PM)metulburr Wrote: i dont get that window....but im in linux. Im sure there is a flag somewhere to stop it.

https://stackoverflow.com/questions/25871898/how-to-hide-chromedriver-console-window

Quote:
It shouldnt matter. driver.get() will open the site. If you are on your current tab, it will change it, if you switch to a new tab, it will open it in that tab, and so on and so on. You are opening each and every tab, so you would know how many you open and if you need more.

I am successful to hide the Console of ChromeDriver
Kindly modify the source code at
..\Python37\Lib\site-packages\selenium\webdriver\common\service.py
And allocate the code below and begin modify
def start(self):
        """
        Starts the Service.

        :Exceptions:
         - WebDriverException : Raised either when it can't start the service
           or when it can't connect to the service
        """
        try:
            cmd = [self.path]
            cmd.extend(self.command_line_args())
            self.process = subprocess.Popen(cmd, env=self.env,
                                            close_fds=platform.system() != 'Windows',
                                            stdout=self.log_file,
                                            stderr=self.log_file,
                                            creationflags=0x08000000, ## To hide Console Window
                                            stdin=PIPE)
Yeah. Because I am using several files to design the automate script, and using 3rd time software to design the application GUI.
Therefore, the GUI have several fancy buttons, each buttons will trigger different .exe file (Currently stuck on covert .py to .exe)
For example, btnGoogle will trigger googleAutomate.exe, btnYahoo will trigger yahooAutomate.exe, and so on.

So my concern is how to check if chrome running/exist? If so then how to launch new tab?

For example Code:
## This is googleAutomate.exe
## CODE TO CHECK IF CHROME RUNNING/EXIST
## IF EXIST THEN DON'T NEED LAUNCH CHROME DRIVER AGAIN
## BUT TO ASSIGN THE CHROME TO driver
driver = webdriver.Chrome(executable_path=r'chromedriver.exe')
driver.get("https://www.google.com")
## This is yahooAutomate.exe
## CODE TO CHECK IF CHROME RUNNING/EXIST
## IF EXIST THEN DON'T NEED LAUNCH CHROME DRIVER AGAIN
## BUT TO ASSIGN THE CHROME TO driver
driver = webdriver.Chrome(executable_path=r'chromedriver.exe')
driver.get("https://malaysia.yahoo.com/?p=us")



RE: Chromedriver launch new tab - metulburr - Feb-13-2019

(Feb-13-2019, 02:08 PM)gahhon Wrote: So my concern is how to check if chrome running/exist? If so then how to launch new tab?
you only ever need to do this ever once in your program
driver = webdriver.Chrome(executable_path=r'chromedriver.exe')
Whereas driver.get(URL) you have to do numerous times to open each tab. Ive already explained how to switch tabs, and how to open a new tab earlier. driver.get(URL) will open the first tab if chrome is not open. If chrome is already open with a tab, then it will load the url on the first tab again with driver.get(url) regardless. To make a new tab you just switch windows before driver.get(url). The previous tab will remain until you close it and you will load and switch focus to the new tab. So there is no need for a check if chrome is running as driver.get(url) will open the first tab or a new tab depending on which window you are on.

As a side note. chromedriver will open a separate process than your chrome browser you are using. It is not meant to piggyback on your browser but open a new one being handled by selenium. chromedriver will have no relation to chrome on your computer.

# Opens a new tab
driver.execute_script("window.open()")
 
# Switch to the newly opened tab
driver.switch_to.window(driver.window_handles[1])
 
# Navigate to new URL in new tab
driver.get("https://google.com")
# Run other commands in the new tab here
 
You're then able to close the original tab as follows
 
# Switch to original tab
driver.switch_to.window(driver.window_handles[0])
 
# Close original tab
driver.close()
 
# Switch back to newly opened tab, which is now in position 0
driver.switch_to.window(driver.window_handles[0])
 
Or close the newly opened tab
 
# Close current tab
driver.close()
 
# Switch back to original tab
driver.switch_to.window(driver.window_handles[0])



RE: Chromedriver launch new tab - gahhon - Feb-14-2019

(Feb-13-2019, 10:35 PM)metulburr Wrote:
(Feb-13-2019, 02:08 PM)gahhon Wrote: So my concern is how to check if chrome running/exist? If so then how to launch new tab?
you only ever need to do this ever once in your program
driver = webdriver.Chrome(executable_path=r'chromedriver.exe')
Whereas driver.get(URL) you have to do numerous times to open each tab. Ive already explained how to switch tabs, and how to open a new tab earlier. driver.get(URL) will open the first tab if chrome is not open. If chrome is already open with a tab, then it will load the url on the first tab again with driver.get(url) regardless. To make a new tab you just switch windows before driver.get(url). The previous tab will remain until you close it and you will load and switch focus to the new tab. So there is no need for a check if chrome is running as driver.get(url) will open the first tab or a new tab depending on which window you are on.

As a side note. chromedriver will open a separate process than your chrome browser you are using. It is not meant to piggyback on your browser but open a new one being handled by selenium. chromedriver will have no relation to chrome on your computer.

# Opens a new tab
driver.execute_script("window.open()")
 
# Switch to the newly opened tab
driver.switch_to.window(driver.window_handles[1])
 
# Navigate to new URL in new tab
driver.get("https://google.com")
# Run other commands in the new tab here
 
You're then able to close the original tab as follows
 
# Switch to original tab
driver.switch_to.window(driver.window_handles[0])
 
# Close original tab
driver.close()
 
# Switch back to newly opened tab, which is now in position 0
driver.switch_to.window(driver.window_handles[0])
 
Or close the newly opened tab
 
# Close current tab
driver.close()
 
# Switch back to original tab
driver.switch_to.window(driver.window_handles[0])

I am understand what you are talking about. But I think you still don't understand what I am try to achieve.
Let's get it simple,
let's say at first I already execute the google.py and it will launch a chrome browser and redirect to http://www.google.com
Then how can i use yahoo.py to control the chrome browser opened by google.py?
So that I can use yahoo.py to open new tab and redirect to http://www.yahoo.com


RE: Chromedriver launch new tab - metulburr - Feb-15-2019

Quote:Then how can i use yahoo.py to control the chrome browser opened by google.py?
It would be a separate process if thats the way you did it. But it depends on how you programmed it to communicate back and forth. If you plan on having it in the background, then it wouldnt matter except for more resource use. However, I would just merge them all; and handle each button press with adding a new tab of whatever that button was associated with to a single browser.


RE: Chromedriver launch new tab - gahhon - Feb-16-2019

(Feb-15-2019, 04:56 PM)metulburr Wrote:
Quote:Then how can i use yahoo.py to control the chrome browser opened by google.py?
It would be a separate process if thats the way you did it. But it depends on how you programmed it to communicate back and forth. If you plan on having it in the background, then it wouldnt matter except for more resource use. However, I would just merge them all; and handle each button press with adding a new tab of whatever that button was associated with to a single browser.

At first my planning is that if my GUI design is based on Python.
But due to Python can't create fancy GUI, so I use another software to design the GUI.
Then the buttons in the GUI will trigger the .exe file. But I am not sure that button can trigger particular functions only. :/