Python\Opera incognito\ private mode - python

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)

Related

How to read url on active tab of browser

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.

How to install and download FF in code view? For Mac on Pycharm

Sorry for pretty stupid question, but I am noob in python.
I got the task to write code that installs and downloads Mozilla Firefox. I code on Mac.
For now I wrote the following:
import os
import urllib
import time
def download():
urllib.urlretrieve('https://download.mozilla.org/?product=firefox-latest&os=win&lang=en-US',
r'/Users/user/Downloads/Firefox 74.0.dmg')
def install():
os.system("cd /Users/user/Downloads/")
time.sleep(15)
os.system("hdiutil mount Firefox\ 74.0.dmg")
time.sleep(15)
os.system("open /Volumes/Firefox/")
time.sleep(15)
def main():
download()
install()
main()
But that's it. I received a lot of pop-up windows and I don't know how to move forward.
Maybe, someone know how execute it?

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.

How can I serve files with UTF-8 encoding using Python SimpleHTTPServer?

I often use the following to quickly fire up a web server to serve HTML content from the current folder (for local testing):
python -m SimpleHTTPServer 8000
Is there a reasonably simple way I can do this, but have the server serve the files with a UTF-8 encoding rather than the system default?
Had the same problem, the following code worked for me.
To start a SimpleHTTPServer with UTF-8 encoding, simply copy/paste the following in terminal (for Python 2).
python -c "import SimpleHTTPServer; m = SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map; m[''] = 'text/plain'; m.update(dict([(k, v + ';charset=UTF-8') for k, v in m.items()])); SimpleHTTPServer.test();"
Ensure that you have the correct charset in your HTML files beforehand.
EDIT: Update for Python 3:
python3 -c "from http.server import test, SimpleHTTPRequestHandler as RH; RH.extensions_map={k:v+';charset=UTF-8' for k,v in RH.extensions_map.items()}; test(RH)"
The test function also accepts arguments like port and bind so that it's possible to specify the address and the port to listen on.
You can run it with Python scripts too.
from functools import partial
from http.server import SimpleHTTPRequestHandler, test
import os
print('http://localhost:8000/')
wk_dir = os.getcwd()
SimpleHTTPRequestHandler.extensions_map = {k: v + ';charset=UTF-8' for k, v in SimpleHTTPRequestHandler.extensions_map.items()}
test(HandlerClass=partial(SimpleHTTPRequestHandler, directory=wk_dir), port=8000, bind='')
Since test is not in http.server.__all__ so IDE may show a warning, and if you don't want to see it, you can use importlib instead of it. for example:
import importlib
http_server = importlib.import_module('http.server')
http_server.test(HandlerClass=partial(SimpleHTTPRequestHandler, directory=wk_dir), port=8000, bind='')

get the windows installation drive using python

How to detect the windows installation path or drive using python code ?
>>> import os
>>> os.environ['SYSTEMDRIVE']
'C:'
You can use GetWindowsDirectory via the ctypes library to get the location of the Windows folder, and then you can use os.path.splitdrive to get the drive letter. For example:
import ctypes
import os
kernel32 = ctypes.windll.kernel32
windows_directory = ctypes.create_unicode_buffer(1024)
if kernel32.GetWindowsDirectoryW(windows_directory, 1024) == 0:
# Handle error
else:
windows_drive = os.path.splitdrive(windows_directory)[0]
You can use the WINDIR enviroment variable.
os.environ['WINDIR']
Use this code to just get the letter, and nothing else:
import os
os.environ['WINDIR'].split(":\\")[0]
Example output:
>>> os.environ['WINDIR'].split(":\\")[0]
'C'

Categories

Resources