Using Python to start a browser (Chromium) and change the url

ぃ、小莉子 提交于 2019-12-11 12:48:32

问题


I am trying to write a script that makes it possible to change the url of an active process.

So for instance, I am starting my browser using:

browser = Popen(["chromium", "http://www.google.com"])

After an X interval I want to change the url of browser.

I've tried allot of things to get this result but nothing has succeeded so far. (stdin.write / put (PIPE) etc etc.).

browser = sh.Command('uzbl-browser')(print_events=True, config='-', uri=current_browser_url, _bg=True)
browser.process.stdin.put('uri http://www.google.nl' + '\n')

I'm hoping you can help me out with this.

Regards,

Wesley.


回答1:


I recommend using selenium to automate this process although you could use webbrowser too:

from selenium.webdriver import *;

chrome = Chrome() # create browser
chrome.get('http://www.google.com')



回答2:


Just import webbrowser and use function webbrowser.open

Here's an example of opening a music playlist:

import webbrowser
gaana= 'http://gaana.com/playlist/gaana-dj-bollywood-top-50-1'
webbrowser.open_new_tab(gaana)



回答3:


i am working on ubuntu 16.04 and i solve this problem by using geckodriver.exe file. these steps are very simple please read caefully.

:: at first you have to install selenium by typing this command on terminal>>

        for python2:-  python -m pip install --user selenium
        for python3:-  python3 -m pip install --user selenium

:: next step download geckodriver using link given below >>

       https://github.com/mozilla/geckodriver/releases

:: since i am using ubuntu so i download geckodriver-v0.24.0-linux64.tar.gz
now extract it.

:: now in the python code for firefox browsing add these lines >>

 from selenium import webdriver

 browser = webdriver.Firefox(executable_path = '/home/aman/Downloads/geckodriver')
 url = str(raw_input("enter a valid url :: "))
 browser.get(url) #example :: url = https://www.google.com
 browser.close()

::for chrome browser >>

 from selenium import webdriver

 browser = webdriver.chrome(executable_path = '/home/aman/Downloads/geckodriver')
 url = str(raw_input("enter a valid url :: "))
 browser.get(url) #example :: url = https://www.google.com
 browser.close()

:: in my pc i extract geckodriver in /home/aman/Downloads/geckodriver so you have to put whole path for geckodriver file where you extract your file.

:: now run this python file on python2.7,for python3 replace raw_input with input. i hope this will definitely work for you.



来源:https://stackoverflow.com/questions/30123632/using-python-to-start-a-browser-chromium-and-change-the-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!