How to read url on active tab of browser - python

I am using python 2.7 but am not able to get anything how to read url which are active in tab of browsers.
So far i tried this
import win32gui
import win32process
import psutil
#w=win32gui
#value=w.GetWindowText(w.GetForegroundWindow())
#print(value)
appwindow = win32gui.GetForegroundWindow()
ProcessID = win32process.GetWindowThreadProcessId(appwindow)
#procname = psutil.Process(ProcessID)
#applicname = procname.name()
print("hello")

Check out pybrinf package, is Windows supported and allows you to get some info about a browser.

Related

Python\Opera incognito\ private mode

I can not seem to find any documentation on how to open Opera in incognito mode
Could anyone help me with this please?
this is the part of code i need to include to open opera , but i have no idea how to inlcude the incognito mode
`import os
import webbrowser
os.startfile(C:\\Program Files\\Opera\\launcher.exe)
webbrowser.open`
Use os or subprocess to run the opera luncher with argument --private
somthing like this:
"C:\<path to opera folder>\Opera\launcher.exe" --private
Full code:
import subprocess
command = r'"C:\<path to opera folder>\Opera\launcher.exe" --private'
subprocess.Popen(command)

Python : No module webkit on windows 10

I am working on a Python application on Windows10 in which I want to use Webkit module from Python. I am getting an error as no module named webkit. I have installed pygtk and python, but I cannot find anything for webkit. Any ideas?
Code :
import os
import urlparse
import webkit
import gtk
view = webkit.WebView()
sw = gtk.ScrolledWindow()
sw.add(view)
win = gtk.Window(gtk.WINDOW_TOPLEVEL)
win.add(sw)
win.resize(1080,1920)
win.show_all()
uri = 'resources/football.html'
uri = os.path.realpath(uri)
uri = urlparse.ParseResult('file', '', uri, '', '', '')
uri = urlparse.urlunparse(uri)
view.load_uri(uri)
gtk.main()
Thank you.
Firstly pygtk is a dead project and you shouldn't use it; PyGObject is the maintained bindings.
Secondly only a very old version of WebKitGtk supports Windows that is terribly insecure and unmaintained so you shouldn't use it.
So sadly WebKitGtk does not support Windows.

Python or LibreOffice Save xlsx file encrypted with password

I am trying to save an Excel file encrypted with password. I have tried following the guide on https://help.libreoffice.org/Common/Protecting_Content_in - and works perfectly. However, this is in the GUI, but I am looking for a solution using the command line interface in headless mode.
I have looked at the man libreoffice, but I could not find anything in there.
Likewise I have looked at the documentation of the Python 3 library openpyxl, but I did not find anything useful there either.
Is it possible to save an Excel 2007+ file encrypted with a password on Ubuntu 14.04/16.04 using the command line (or Python library) that do not require any user interaction or X session?
There is solution using Jython and Apache POI. If you want like to use it from CPython/PyPy, you can use subprocess module to call external Jython script.
I assume that you have Java JRE/JDK installed
Create non-encrypted xlsx file with Excel/Calc or use xlsxwriter or openpyxl and save it as test1.xlsx
Download standalone Jython
Download Apache POI
Extract Apache POI in same dir where is standalone Jython jar
Save following Jython script as encrypt.py:
import os
import sys
from java.io import BufferedInputStream
from java.io import FileInputStream
from java.io import FileOutputStream
from java.io import File
from java.io import IOException
from org.apache.poi.poifs.crypt import EncryptionInfo, EncryptionMode
from org.apache.poi.poifs.crypt import CipherAlgorithm, HashAlgorithm
from org.apache.poi.poifs.crypt.agile import AgileEncryptionInfoBuilder
from org.apache.poi.openxml4j.opc import OPCPackage, PackageAccess
from org.apache.poi.poifs.filesystem import POIFSFileSystem
from org.apache.poi.ss.usermodel import WorkbookFactory
def encrypt_xlsx(in_fname, out_fname, password):
# read
in_f = File(in_fname)
in_wb = WorkbookFactory.create(in_f, password)
in_fis = FileInputStream(in_fname)
in_wb.close()
# encryption
out_poi_fs = POIFSFileSystem()
info = EncryptionInfo(EncryptionMode.agile)
enc = info.getEncryptor()
enc.confirmPassword(password)
opc = OPCPackage.open(in_f, PackageAccess.READ_WRITE)
out_os = enc.getDataStream(out_poi_fs)
opc.save(out_os)
opc.close()
# write
out_fos = FileOutputStream(out_fname)
out_poi_fs.writeFilesystem(out_fos)
out_fos.close()
if __name__ == '__main__':
in_fname = sys.argv[1]
out_fname = sys.argv[2]
password = sys.argv[3]
encrypt_xlsx(in_fname, out_fname, password)
Call it from console:
java -cp "jython-standalone-2.7.0.jar:poi-3.15/lib/commons-codec-1.10.jar:poi-3.15/lib/commons-collections4-4.1.jar:poi-3.15/poi-3.15.jar:poi-3.15/poi-ooxml-3.15.jar:poi-3.15/poi-ooxml-schemas-3.15.jar:poi-3.15/ooxml-lib/curvesapi-1.04.jar:poi-3.15/ooxml-lib/xmlbeans-2.6.0.jar" org.python.util.jython -B encrypt.py test1.xlsx test1enc.xlsx 12345678
Where:
encrypt.py - name of script
test1.xlsx - input filename
test1enc.xlsx - output filename
12345678 - password
Final encrypted xslx should be in test1enc.xlsx.

Can't use mouse related function via LDTP(Python)

I am trying to integrate LDTP and selenium in python environment for automation upload an image to "http://imgure.com/"
My environment as below:
OS: Ubuntu 15.10
Python: v3.5/v2.7
IDE: Pycram 2016.1
LDTP: v3.5
from selenium import webdriver
import ldtp
def main():
browser = webdriver.Firefox()
browser.implicitly_wait(5000)
browser.get("http://imgur.com/")
browser.maximize_window()
browser.find_element_by_css_selector("li.upload-button-container").click()
browser.find_element_by_css_selector("div#upload-global-file-wrapper").click()
ldtp.maximizewindow("File Upload")
#Call LDTP's mouserightclick function
ldtp.mouseleftclick('File Upload', 'btnOpen')
browser.find_element_by_css_selector("button#upload-global-start-button.button-big.green").click()
if __name__ == '__main__':
main()
My Pycram screenshot:
Pycram screenshot
Pycram can't find mouseleftclick function when i try to use mouseleftclick.
Does anyone know how to fix this issue? Thanks.

Opening files with Pydbg while application is running

Using pydbg I'm opening files(ex. c:\\myfile.mnp) within a win32 application(Ex. c:\\myprog.exe) in this way.
dbg = pydbg()
dbg.load("c:\\myprog.exe", "c:\\myfile1.mnp")
If the target application is already running then, is it possible to open a another file(For example c:\myfile2.mnp ) within the same application which is already running without closing that process/apps, using pydbg?
From personal experience, its better to have python start the application, or attach to it while its running.
import pydbg
from pydbg import *
from pydbg.defines import *
import struct
import utils
dbg = pydbg()
pid = ''
name = ''
found_program = False
for (pid, name) in dbg.enumerate_processes():
if name.lower() == "program.exe":
found_program = True
dbg.attach(pid)
if found_program:
dbg.run()
To make python start it:
from os import system
system('start "c:\program.exe"')

Categories

Resources