0

I am not taking about headless browser.

I am writing an script, when a directory get a new file, it will open an url, when the file deleted from directory, it will open another url.

My Script is below:

import os, time


def folderListener(mydir):
    old_list = len(os.listdir(mydir))
    new_list = 0
    while True:
        new_list = len(os.listdir(mydir))
        if old_list != new_list:
            old_list = new_list
            print('OPen first url in chrom browser')
        else:
            print('Open 2nd URL in Chrome Browser')
        time.sleep(2)

if __name__ == '__main__':
    mydir = os.getcwd() + '/testdir'
    folderListener(mydir)

The script working very nice, now i need to it in the script when print function place. i mean, it should open an URL on Chrome Browser instead, no headless chrome browser.

1
  • os.startfile(url) (probably only Windows), os.system("chrome url"), webbrowser.open(url), selenium.webdriver.Chrome().get(url) Commented Apr 27, 2019 at 3:38

1 Answer 1

1

You could look into webbrowser.

Used like:

import webbrowser

url = "http://mywebsite.com"

webbrowser.open_new(url) # open in default browser
webbrowser.get('safari').open_new(url) # open in safari if available
webbrowser.get('firefox').open_new_tab(url) # open in firefox if available
webbrowser.get(using='google-chrome').open_new(url) # open in chrome if available

But considering you tagged selenium-webdriver you could also do this:

from selenium import webdriver

url = "http://mywebsite.com"

driver = webdriver.Chrome()
driver.get(url)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.