I have question about possibility to get url of all opened tabs in Edge browser.
I have started with pywinauto and I have possibility to get all tab names from Chrome or from Edge:
from pywinauto import Desktop
desktop = Desktop(backend="uia")
window = desktop.windows(title_re="edge", control_type="Window")[0] # change title_re to "chrome" to get values for chrome browser
wrapper_list = window.descendants(control_type="TabItem")
tab_names = [tab.window_text() for tab in wrapper_list]
And yes, I'm getting all opened Edge tabs.
I have also checked the code below:
import pywinauto
app = pywinauto.Application(backend='uia')
app.connect(title_re=".*Microsoft​ Edge.*", found_index=0)
dlg = app.top_window()
wrapper = dlg.child_window(title="App bar", control_type="ToolBar")
url = wrapper.descendants(control_type='Edit')[0]
print(url.get_value())
With no success - the "url = wrapper.descendants(control_type='Edit')[0]" returns error:
"ElementNotFound"
So... how to use it to get url of all the opened tabs? Or just one by using it's title?
It's not necessary to use pywinauto of course.
Have a nice day!
there is more easier way to do that, and it do more, for example,get title of each tab:
from clicknium import clicknium as cc
if not cc.edge.extension.is_installed():
cc.edge.extension.install_or_update()
for browser in cc.edge.browsers:
for tab in browser.tabs:
print(tab.url)
Related
I'm writing out a code that opens up all necessary tabs using the webbrowser module so that I don't have to open them all manually when I open my pc, but one of the links I want it to open in incognito mode (all the links are google links) is this possible? I don't want all the code to open on incognito mode I just want the Microsoft Teams URL to open in incognito. Thanks in advance.
my code:
import webbrowser
import time
webbrowser.open('https://www.youtube.com/')
time.sleep(0.3)
webbrowser.open('https://mail.google.com/')
time.sleep(0.3)
webbrowser.open('https://teams.microsoft.com')
The solution is to specify the browser executable with the appropriate command line arguments to start it in incognito mode. The exact flag may vary by browser. Firefox, for example, takes the -private-window flag to start in incognito mode.
import webbrowser
import time
firefox_path = 'C:/Program Files/Mozilla Firefox/firefox.exe'
incognito_browser = webbrowser.get(firefox_path + ' -private-window %s')
webbrowser.open('https://www.youtube.com/')
time.sleep(0.3)
webbrowser.open('https://mail.google.com/')
time.sleep(0.3)
incognito_browser.open('https://teams.microsoft.com')
# ...could use now incognito_browser to open additional sites as desired.
The '%s' is substituted with the URL during the call to open and must be present for get to recognize that a command line invocation is being supplied rather than a named browser instance, e.g. 'opera'.
I'm trying to use Tor with selenium, which works through the use of tbselenium.
However, when loading an url or clicking a web element, the page immideately closes when finishing the action, instead of remaining open as would be the case when using selenium with chrome.
Any ideas to keep the page open?
import tbselenium.common as cm
from tbselenium.tbdriver import TorBrowserDriver
from tbselenium.utils import launch_tbb_tor_with_stem
tbb_dir = "C:\\pathto\\Tor Browser\\"
tor_process = launch_tbb_tor_with_stem(tbb_path=tbb_dir)
for i in range(1):
with TorBrowserDriver(tbb_dir, tor_cfg=cm.USE_STEM) as driver:
driver.load_url("http://hln.be",3,wait_for_page_body=True)
#driver.get('https://google.be')
try:
policypage=driver.find_element_by_xpath("//a[contains(#href,'members/join')]")
policypage.click()
usern=driver.find_element_by_xpath("//input[contains(#id,'user_member_username')]")
usern.send_keys('Tryout')
except:
print('different look')
As Furas said, use the standard driver declaration.
I'm trying to open a URL in a new window. I can't use selenium because it wouldn't be signed into Google.
My default browser is Chrome using Windows 10 and I already have 3 Chrome windows open each with multiple tabs.
here is my code on Python 3.6:
import webbrowser
url = 'https://google.com'
open_google = webbrowser.open('https://google.com', new=1)
open_google = webbrowser.open_new('https://google.com')
Both of these give me a new tab in my current window instead of a new window. Why is this happening? is it a setting in Chrome?
How about opening a new window using os.system or subprocess and then using webbrowser to get the urls/apps to open there.
Something like:
import subprocess
command = "cmd /c start chrome http://www.google.com --new-window"
subprocess.Popen(command, shell=True)
and then doing:
open_google = webbrowser.open('https://google.com', new=1)
I want to be able to launch different browsers, because they have a different default theme and their displays look different on my computer.
Here is an approach that parameterizes the URL string to dynamically create the URL with the browser name "on the fly".
import subprocess
browser_type = "chrome" # Replace the value with "edge" or "firefox"
location_url = "https://stackoverflow.com/questions/7521729/how-to-open-a-new-default-browser-window-in-python-when-the-default-is-{browser_type}"
command_fstring = f"cmd /c start firefox {location_url} --new-window"
subprocess.Popen(command_string, shell=True)
To give credit where it is due, this is based on the answer by #Sharath on this page: Web browser chrome, can't open URL in new window, keeps opening URL as a tab
How do I print a webpage using selenium please.
import time
from selenium import webdriver
# Initialise the webdriver
chromeOps=webdriver.ChromeOptions()
chromeOps._binary_location = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"
chromeOps._arguments = ["--enable-internal-flash"]
browser = webdriver.Chrome("C:\\Program Files\\Google\\Chrome\\Application\\chromedriver.exe", port=4445, chrome_options=chromeOps)
time.sleep(3)
# Login to Webpage
browser.get('www.webpage.com')
Note: I am using the, at present, current version of Google Chrome: Version 32.0.1700.107 m
While it's not directly printing the webpage, it is easy to take a screenshot of the entire current page:
browser.save_screenshot("screenshot.png")
Then the image can be printed using any image printing library. I haven't personally used any such library so I can't necessarily vouch for it, but a quick search turned up win32print which looks promising.
The key "trick" is that we can execute JavaScript in the selenium browser window using the "execute_script" method of the selenium webdriver, and if you execute the JavaScript command "window.print();" it will activate the browsers print function.
Now, getting it to work elegantly requires setting a few preferences to print silently, remove print progress reporting, etc. Here is a small but functional example that loads up and prints whatever website you put in the last line (where 'http://www.cnn.com/' is now):
import time
from selenium import webdriver
import os
class printing_browser(object):
def __init__(self):
self.profile = webdriver.FirefoxProfile()
self.profile.set_preference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", False)
self.profile.set_preference("pdfjs.disabled", True)
self.profile.set_preference("print.always_print_silent", True)
self.profile.set_preference("print.show_print_progress", False)
self.profile.set_preference("browser.download.show_plugins_in_list",False)
self.driver = webdriver.Firefox(self.profile)
time.sleep(5)
def get_page_and_print(self, page):
self.driver.get(page)
time.sleep(5)
self.driver.execute_script("window.print();")
if __name__ == "__main__":
browser_that_prints = printing_browser()
browser_that_prints.get_page_and_print('http://www.cnn.com/')
The key command you were probably missing was "self.driver.execute_script("window.print();")" but one needs some of that setup in init to make it run smooth so I thought I'd give a fuller example. I think the trick alone is in a comment above so some credit should go there too.
def align_sequences(IDs):
import webbrowser
import urllib,urllib2
url = 'http://www.uniprot.org/align/'
params = {'query':IDs}
data = urllib.urlencode(params)
request = urllib2.Request(url, data)
response = urllib2.urlopen(request)
job_url = response.geturl()
webbrowser.open(job_url)
align_sequences('Q4PRD1 Q7LZ61')
With this function I want to open 'http://www.uniprot.org/align/', request the protein sequences with IDs Q4PRD1 and Q7LZ61 to be aligned, and then open the website in my browser.
Initially it seems to be working fine - running the script will open the website and show the alignment job to being run. However, it will keep going forever and never actually finish, even if I refresh the page. If I input the IDs in the browser and hit 'align' it works just fine, taking about 8 seconds to align.
I am not familiar with the differences between running something directly from a browser and running it from Python. Do any of you have an idea of what might be going wrong?
Thank you :-)
~Max
You have to click align button. You can't do this with webbrowser though. One option is to use selenium:
from selenium import webdriver
url = 'http://www.uniprot.org/align/'
ids = 'Q4PRD1 Q7LZ61'
driver = webdriver.Firefox()
driver.get(url)
q = driver.find_element_by_id('alignQuery')
q.send_keys(ids)
btn = driver.find_element_by_id("sequence-align-submit")
btn.click()
I think this is in javascript. If you look at the html-code of button Align you can see
onclick="UniProt.analytics('AlignmentSubmissionPage', 'click', 'Submit align'); submitAlignForm();"
UniProt.analytics() and submitAlignForm() some javascript magic. This magic in js-compr.js2013_11 file.
You can view this file using http://jsbeautifier.org/ and then do on python what do javascript.