How to keep google open using Python webbrowser? - python

I'm trying to open several google pages through python webbrowser, and I've gotten it to work, but after opening all the pages, google simply closes unless I manually click it. Why is this happening? Help is appreciated, and thank in advance.
import pyautogui as py
import keyboard as key
import time
chrome_path="C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
webbrowser.register('chrome',None, webbrowser.BackgroundBrowser(chrome_path))
def n_t():
py.hotkey('ctrl', 't')
def m_t():
py.hotkey('ctrl', 'tab')
def gogle():
webbrowser.get('chrome').open('gmail.com')
py.press('enter')
time.sleep(5)
n_t()
py.write('www.youtube.com')
py.press('enter')
n_t()
py.write('hangouts.google.com/authuser=2')
py.press('enter')
n_t()
py.write('stackoverflow.com')
py.press('enter')
m_t()
gogle()

Use this:
import webbrowser
webbrowser.open('https://google.com')
webbrowser.open('https://gmail.com')
webbrowser.open('https://youtube.com')
webbrowser.open('https://stackoverflow.com')
and you're good to go.

Related

PywinAuto - Excel Automation Can not click the button

I m making an Excel automation via pywinauto library. But there is a hard challange for me due to using Excel Oracle add-ins called Smartview.
I need to click 'Private Connections' button, however i can't find any little info in app.Excel.print_control_identifiers() Private Connections
So i tried to use inspector.exe for find ui element regarding private connections button, however i couldn't find any little solvetion inside of inspector.exe's result inspector's result
Then i used another program called UISpy, however i can only find private connection's pane inside of the program. UISpy's result
i tried to find an answer but i couldn't find out anything. So, can you help me to click here?
By the way here is my code :
import pywinauto
from pywinauto import application
from pywinauto.keyboard import send_keys
from pywinauto.controls.common_controls import TreeViewWrapper
program_path = r"C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE"
file_path = r"C:\Users\AytugMeteBeder\Desktop\deneme.xlsx"
app = application.Application(backend="uia").start(r'{} "{}"'.format(program_path, file_path))
# sapp = application.Application(backend="uia").connect(title = 'deneme.xlsx - Excel')
time.sleep(7)
myExcel = app.denemeExcel.child_window(title="Smart View", control_type="TabItem").wrapper_object()
myExcel.click_input()
Panel = app.denemeExcel.child_window(title="Panel", control_type="Button").wrapper_object()
Panel.click_input()
time.sleep(1)
app.denemeExcel.print_control_identifiers()

Upload a file using WebDriver & PyWinAuto

I have a python script that tries to upload a file from my PC to a web application.
I press via WebDriver the specific upload button in the browser and then a Win7 explorer window opens for me to navigate and select the desired file to upload.
How could I manipulate this window with pywinauto?
optional: could this be done in linux as well (with an appropriate library I suppose) ?
This is my sample code:
wd.find_element_by_css_selector("img.editLecturesButtons.fromVideo").click()
#switch to the lightbox
wd.switch_to_frame(int("1"))
#hit upload
wd.find_element_by_xpath("//*[#id='fileUpload']").click()
#TODO
import os,pywinauto.application
file = os.path.normpath("C:\Users\me\Desktop\image.jpg")
....
I agree with Mark, you should try the Webdriver methods. Regard to pywinauto, code may looks like:
import pywinauto
pwa_app = pywinauto.application.Application()
w_handle = pywinauto.findwindows.find_windows(title=u'Open', class_name='#32770')[0]
window = pwa_app.window_(handle=w_handle)
ctrl = window['Name']
ctrl.SetText(file)
ctrl = window['OK']
ctrl.Click()
This sollution only for Windows, since pywinauto uses win32 api.

webbrowser script executes without error, but nothing happens?

Im writing a script which is supposed to open different browser with given urls.
When I run it in eclipse it runs the script without errors, but no browsers open. :/
import webbrowser as wb
url_mf = ['https://www.thatsite.com/','http://thatothersite.org/']
url_gc = ['https://www.thatsite.com/','http://thatothersite.org/']
chrome = wb.get('/usr/bin/google-chrome %s')
firefox = wb.get('fierfox %s')
chrome.open(url_gc[1], new=1)
firefox.open(url_mf[1], new=1)
I also have a script using the IEC.py module to open Internet explorer (I need to enter login info and, later, extract horribly unformatted db queries from a site - mechanize & selenium seemed a bit over the top for that?), and that works just fine. But I'm guessing that's like comparing apples and oranges?
import iec
ie= iec.IEController()
ie.Navigate(url_ie[1])
Any help is very much appreciated.
First thing I noticed is the typo on line 5. It should be Firefox instead of fierfox. Second thing, I ran your code in SublimeText 2, I had no problems, I changed the paths because I'm on a windows machine.
The code below opened both Firefox and Chrome.
import webbrowser as wb
url_mf = ['https://www.thatsite.com/','http://www.google.ie/']
url_gc = ['https://www.thatsite.com/','http://www.google.ie/']
chrome = wb.get('"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" %s')
firefox = wb.get('"C:/Program Files (x86)/Mozilla Firefox/firefox.exe" %s')
chrome.open(url_gc[1], new=1)
firefox.open(url_mf[1], new=1)
Do you really want to specify which browser the program wants to use ?, I'd suggest using
import webbrowser as wb
urls = ["http://www.google.ie/","http://www.gametrailers.com/"]
for url in urls:
wb.open(url,new=2, autoraise=True)
This would just get your default browser and open each of the links in new tabs.

python webkit webview remember cookies?

I have written a short python script that opens Google music in web view window. however I can't seem to find anything about getting webkit to use cookies so that I don't have to login every time I start it up.
Here's what I have:
#!/usr/bin/env python
import gtk, webkit
import ctypes
libgobject = ctypes.CDLL('/usr/lib/i386-linux-gnu/libgobject-2.0.so.0')
libwebkit = ctypes.CDLL('/usr/lib/libsoup-2.4.so.1')
libsoup = ctypes.CDLL('/usr/lib/libsoup-2.4.so.1')
libwebkit = ctypes.CDLL('/usr/lib/libwebkitgtk-1.0.so.0')
proxy_uri = libsoup.soup_uri_new('http://tcdproxy.tcd.ie:8080') #proxy urli
session = libwebkit.webkit_get_default_session()
libgobject.g_object_set(session, "proxy-uri", proxy_uri, None)
w = gtk.Window()
w.connect("destroy",w.destroy)
w.set_size_request(1000,600)
w.connect('delete-event', lambda w, event: gtk.main_quit())
s = gtk.ScrolledWindow()
v = webkit.WebView()
s.add(v)
w.add(s)
w.show_all()
v.open('http://music.google.com')
gtk.main()
Any help on this would be greatly appreciated,
thanks,
Richard
Worked it out, but it required learning more ctypes than I wanted -_-. Try this- I required different library paths, etc than you, so I'll just paste what's relevant.
#remove all cookiejars
generic_cookiejar_type = libgobject.g_type_from_name('SoupCookieJar')
libsoup.soup_session_remove_feature_by_type(session, generic_cookiejar_type)
#and replace with a new persistent jar
cookiejar = libsoup.soup_cookie_jar_text_new('/path/to/your/cookies.txt',False)
libsoup.soup_session_add_feature(session, cookiejar)
The code's pretty self explanatory. There's also a SoupCookieJarSqlite that you might prefer, though I'm sure the text file would be easier for development.
EDIT: actually, the cookie jar removal doesn't seem to be doing anything, so the appropriate snippet is
#add a new persistent cookie jar
cookiejar = libsoup.soup_cookie_jar_text_new('/path/to/your/cookies.txt',False)
libsoup.soup_session_add_feature(session, cookiejar)
I know its old question and have been looking for the answer all over the place. Finally came up on my own after some trial and error. Hope this helps others.
This is basically same answer from Matt, just using GIR introspection and feels more pythonish.
from gi.repository import Soup
cookiejar = Soup.CookieJarText.new("<Your cookie path>", False)
cookiejar.set_accept_policy(Soup.CookieJarAcceptPolicy.ALWAYS)
session = WebKit.get_default_session()
session.add_feature(cookiejar)
In the latest version i.e. GTK WebKit2 4.0, this has to be done in the following way:
import gi
gi.require_version('Soup', '2.4')
gi.require_version('WebKit2', '4.0')
from gi.repository import Soup
from gi.repository import WebKit2
browser = WebKit2.WebView()
website_data_manager = browser.get_website_data_manager()
cookie_manager = website_data_manager.get_cookie_manager()
cookie_manager.set_persistent_storage('PATH_TO_YOUR/cookie.txt', WebKit2.CookiePersistentStorage.TEXT)
cookie_manager.set_accept_policy(Soup.CookieJarAcceptPolicy.ALWAYS)

Flash-Selenium and Python

I want to try Flash-Selenium with the python driver, however I have some concerns regarding the available python extension, it seems aged and there is no example on how to use it... Is there anybody who is using it? Any example on how to use it ?
Example taken from FlashSelenium page:
from com.thoughtworks.selenium.FlashSelenium import FlashSelenium
from com.thoughtworks.selenium.selenium import selenium
url = "http://flashselenium.t35.com/colors.html"
browserType = "*firefox"
selenium = selenium("localhost", 4444, browserType, url)
selenium.start()
selenium.open(url)
flashApp = FlashSelenium(selenium, "coloredSquare")
flashApp.percent_loaded()

Categories