Pywinauto 0.6.0 access browser URL in edit control for Firefox - python-2.7

I am currently trying to access URL of the active window for Firebox browser using Pywinauto 0.6.0, as part of a python app that can track website usage. I am a newbie python programmer, so if I have made some obvious mistakes then this is why.
I have read all the material I can find on Google and the Pywinauto docs, but there are no clear indications of how to do this, atleast not without using "Typekeys" after connecting to a window.
In fact, I have been able to access to the URL the "hack" way by connecting to the browser window with pywinauto, then using "TypeKeys()" to grab the URL and copy it to clipboard. But this approach will not work for me, as it interrupts the user and my app must run in the background whilst the user is accessing their system as usual. Using the typekeys method introduces some odd mouse and window behaviour when changing or refreshing windows and then trying to grab the URL - so the approach has proven unworkable for me.
Currently the code I have is as follows (I have used the window text title='' of a specific window I am using for testing, in practice it is whichever the active tab on the browser is):
from pywinauto import *
app = application.Application()
app.connect(title=u'Facebook - Log In or Sign Up - Mozilla Firefox', found_index=0)
window = app.top_window_()
window.PrintControlIdentifiers()
titlebar = window.child_window(title=u'Facebook - Log In or Sign Up - Mozilla Firefox')
toolbar = titlebar.child_window(title=u'Navigation Toolbar')
combobox = toolbar.child_window(title=u'Search or enter address')
edit = combobox.child_window(title=u'Search or enter address')
I use PrintControlIdentifiers() in order to see which elements I can interact with, but this returns only
MozillaWindowClass - 'Facebook - Log In or Sign Up - Mozilla Firefox' (L-32000, T-32000, R-31840, B-31944)
[u'Facebook - Log In or Sign Up - Mozilla Firefox', u'MozillaWindowClass', u'Facebook - Log In or Sign Up - Mozilla FirefoxMozillaWindowClass']
I can access the Firefox Window fine with the current active tab, but next when trying to access the ChildWindow and subsequent ChildWindow there is no error, until I try and do something with the childwindow e.g., Click() function on what I think in the URL bar UI element. However, I am not sure even if the code is even accessing the child window element correctly in the first place. Or if this the right way to try and filter through the child elements to get to the URL edit control element.
As can be seen in the below image of the tree view of the UI elements (using UISPY.exe), the following tree to access the firefox browser edit control element that contains the URL is:
Tree view of Firefox UI elements via UISPY
"window" -> "title bar" -> "tool bar" "Navigation Toolbar" -> "combo box" -> "Search or enter address" -> "edit" "Search or enter address
where the "edit" control contains the attribute Value, Value:"url", which I need to extract to a variable.
Any help with this would greatly appreciated.

This is probably too late to post but incase any future developers has similiar question. Here is working solution.
Just pass backend='uia' as argument like application.Application(backend='uia'). Now, accessing and performing actions on child_window is possible.
Here is complete code snippet for any url.
url='https://google.com'
app = application.Application(backend='uia').connect(title_re=".*Mozilla Firefox")
main_firefox_winspec = app.window(title_re=".*Mozilla Firefox")
address_bar = main_firefox_winspec.child_window(title_re=".*or enter address", control_type="Edit")
address_bar.set_edit_text(url)
time.sleep(3)
main_firefox_winspec.child_window(title="Go to the address in the Location Bar").wrapper_object().click_input()

Related

Open a c++ application installed on computer with a custom url in browser [duplicate]

How do i set up a custom protocol handler in chrome? Something like:
myprotocol://testfile
I would need this to send a request to http://example.com?query=testfile, then send the httpresponse to my extension.
The following method registers an application to a URI Scheme. So, you can use mycustproto: in your HTML code to trigger a local application. It works on a Google Chrome Version 51.0.2704.79 m (64-bit).
I mainly used this method for printing document silently without the print dialog popping up. The result is pretty good and is a seamless solution to integrate the external application with the browser.
HTML code (simple):
Click Me
HTML code (alternative):
<input id="DealerName" />
<button id="PrintBtn"></button>
$('#PrintBtn').on('click', function(event){
event.preventDefault();
window.location.href = 'mycustproto:dealer ' + $('#DealerName').val();
});
URI Scheme will look like this:
You can create the URI Scheme manually in registry, or run the "mycustproto.reg" file (see below).
HKEY_CURRENT_USER\Software\Classes
mycustproto
(Default) = "URL:MyCustProto Protocol"
URL Protocol = ""
DefaultIcon
(Default) = "myprogram.exe,1"
shell
open
command
(Default) = "C:\Program Files\MyProgram\myprogram.exe" "%1"
mycustproto.reg example:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Classes\mycustproto]
"URL Protocol"="\"\""
#="\"URL:MyCustProto Protocol\""
[HKEY_CURRENT_USER\Software\Classes\mycustproto\DefaultIcon]
#="\"mycustproto.exe,1\""
[HKEY_CURRENT_USER\Software\Classes\mycustproto\shell]
[HKEY_CURRENT_USER\Software\Classes\mycustproto\shell\open]
[HKEY_CURRENT_USER\Software\Classes\mycustproto\shell\open\command]
#="\"C:\\Program Files\\MyProgram\\myprogram.exe\" \"%1\""
C# console application - myprogram.exe:
using System;
using System.Collections.Generic;
using System.Text;
namespace myprogram
{
class Program
{
static string ProcessInput(string s)
{
// TODO Verify and validate the input
// string as appropriate for your application.
return s;
}
static void Main(string[] args)
{
Console.WriteLine("Raw command-line: \n\t" + Environment.CommandLine);
Console.WriteLine("\n\nArguments:\n");
foreach (string s in args)
{
Console.WriteLine("\t" + ProcessInput(s));
}
Console.WriteLine("\nPress any key to continue...");
Console.ReadKey();
}
}
}
Try to run the program first to make sure the program has been placed in the correct path:
cmd> "C:\Program Files\MyProgram\myprogram.exe" "mycustproto:Hello World"
Click the link on your HTML page:
You will see a warning window popup for the first time.
To reset the external protocol handler setting in Chrome:
If you have ever accepted the custom protocol in Chrome and would like to reset the setting, do this (currently, there is no UI in Chrome to change the setting):
Edit "Local State" this file under this path:
C:\Users\Username\AppData\Local\Google\Chrome\User Data\
or Simply go to:
%USERPROFILE%\AppData\Local\Google\Chrome\User Data\
Then, search for this string: protocol_handler
You will see the custom protocol from there.
Note: Please close your Google Chrome before editing the file. Otherwise, the change you have made will be overwritten by Chrome.
Reference:
https://msdn.microsoft.com/en-us/library/aa767914(v=vs.85).aspx
Chrome 13 now supports the navigator.registerProtocolHandler API. For example,
navigator.registerProtocolHandler(
'web+custom', 'http://example.com/rph?q=%s', 'My App');
Note that your protocol name has to start with web+, with a few exceptions for common ones (like mailto, etc). For more details, see: http://updates.html5rocks.com/2011/06/Registering-a-custom-protocol-handler
This question is old now, but there's been a recent update to Chrome (at least where packaged apps are concerned)...
http://developer.chrome.com/apps/manifest/url_handlers
and
https://github.com/GoogleChrome/chrome-extensions-samples/blob/e716678b67fd30a5876a552b9665e9f847d6d84b/apps/samples/url-handler/README.md
It allows you to register a handler for a URL (as long as you own it). Sadly no myprotocol:// but at least you can do http://myprotocol.mysite.com and can create a webpage there that points people to the app in the app store.
This is how I did it. Your app would need to install a few reg keys on installation, then in any browser you can just link to foo:\anythingHere.txt and it will open your app and pass it that value.
This is not my code, just something I found on the web when searching the same question. Just change all "foo" in the text below to the protocol name you want and change the path to your exe as well.
(put this in to a text file as save as foo.reg on your desktop, then double click it to install the keys)
-----Below this line goes into the .reg file (NOT including this line)------
REGEDIT4
[HKEY_CLASSES_ROOT\foo]
#="URL:foo Protocol"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\foo\shell]
[HKEY_CLASSES_ROOT\foo\shell\open]
[HKEY_CLASSES_ROOT\foo\shell\open\command]
#="\"C:\\Program Files (x86)\\Notepad++\\notepad++.exe\" \"%1\""
Not sure whether this is the right place for my answer, but as I found very few helpful threads and this was one of them, I am posting my solution here.
Problem: I wanted Linux Mint 19.2 Cinnamon to open Evolution when clicking on mailto links in Chromium. Gmail was registered as default handler in chrome://settings/handlers and I could not choose any other handler.
Solution:
Use the xdg-settings in the console
xdg-settings set default-url-scheme-handler mailto org.gnome.Evolution.desktop
Solution was found here https://alt.os.linux.ubuntu.narkive.com/U3Gy7inF/kubuntu-mailto-links-in-chrome-doesn-t-open-evolution and adapted for my case.
I've found the solution by Jun Hsieh and MuffinMan generally works when it comes to clicking links on pages in Chrome or pasting into the URL bar, but it doesn't seem to work in a specific case of passing the string on the command line.
For example, both of the following commands open a blank Chrome window which then does nothing.
"c:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "foo://C:/test.txt"
"c:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --new-window "foo://C:/test.txt"
For comparison, feeding Chrome an http or https URL with either of these commands causes the web page to be opened.
This became apparent because one of our customers reported that clicking links for our product from a PDF being displayed within Adobe Reader fails to invoke our product when Chrome is the default browser. (It works fine with MSIE and Firefox as default, but not when either Chrome or Edge are default.)
I'm guessing that instead of just telling Windows to invoke the URL and letting Windows figure things out, the Adobe product is finding the default browser, which is Chrome in this case, and then passing the URL on the command line.
I'd be interested if anyone knows of Chrome security or other settings which might be relevant here so that Chrome will fully handle a protocol handler, even if it's provided via the command line. I've been looking but so far haven't found anything.
I've been testing this against Chrome 88.0.4324.182.
open
C:\Users\<Username>\AppData\Local\Google\Chrome\User Data\Default
open Preferences then search for excluded_schemes you will find it in 'protocol_handler' delete this excluded scheme(s) to reset chrome to open url with default application

How To set aria-pressed="true" in Selenium?

HTML:-
<ion-segment-button class="segment-button" role="button" tappable value="sign_in" aria-pressed="false">
Selenium Code:-
WebDriver driver = new ChromeDriver();
driver.get("URL");
WebElement Login =driver.findElement(ByClassName.className("segment-button"));
Login.submit();
How To set aria-pressed="true" in Selenium??
There is no direct way to set the attribute of a element in selenium. But we can use the javascript Executor to set attribute of a element as given below.
WebDriver driver = new ChromeDriver();
driver.get("URL");
WebElement Login =driver.findElement(ByClassName.className("segment-button"));
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("arguments[0].setAttribute('aria-pressed',true);",Login);
The short answer is don't. Don't do this unless you really know what you are doing and understand the site. Forcing values on elements using JS shortcuts all the events that the site has to handle this user flow and you could put the page in a bad state. No user is going to run JS on the page to put it in the desired state... your script shouldn't either.
The right way to do this is to figure out how this gets set from a user's perspective. What buttons do I have to push, etc. to get the desired setting? Once you figure that out, write the Selenium code to push those buttons, etc. and you will have the desired result.

Is there a way to select pictures in a pop-up using Python Selenium?

I am able to do all the basic web actions using Selenium, but I would like to do the following:
Click on a "select image" button, then there comes a pop-up, then I would like to enter some text in #1 and #2, and click #3.
Edition: What I have tried is on kijiji site, after clicking the select image button, I select two images (see picture), then I backward locate these image holder and find the Xpath, then I added the following codes (below), but it doesn't seem to do anything, it gave me some stacktrace message and no error.
imageLoc = "C:\AbsolutePath\IMG_20150620_184908_Edited_sm.jpg"
imageHolderXpath = '(//div[#class="image"])[1]'
imageHolderElement = WebDriverWait(driver, 5).until(lambda driver: driver.find_element_by_xpath(imageHolderXpath))
imageHolderElement.send_keys(imageLoc)
Output:
Message:
Stacktrace:
at FirefoxDriver.prototype.findElementInternal_ (file:///c:/users/kubik/appdata/local/temp/tmp_sthxl/extensions/fxdriver#googlecode.com/components/driver-component.js:10659)
at FirefoxDriver.prototype.findElement (file:///c:/users/kubik/appdata/local/temp/tmp_sthxl/extensions/fxdriver#googlecode.com/components/driver-component.js:10668)
at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/kubik/appdata/local/temp/tmp_sthxl/extensions/fxdriver#googlecode.com/components/command-processor.js:12534)
at DelayedCommand.prototype.executeInternal_ (file:///c:/users/kubik/appdata/local/temp/tmp_sthxl/extensions/fxdriver#googlecode.com/components/command-processor.js:12539)
at DelayedCommand.prototype.execute/< (file:///c:/users/kubik/appdata/local/temp/tmp_sthxl/extensions/fxdriver#googlecode.com/components/command-processor.js:12481)
[Finished in 30.3s]
It is not possible via selenium, the Upload File dialog is out of selenium's reach.
By all means, you should avoid this popup being opened in the first place. A common solution to this problem is to find the corresponding file input element and send the absolute path to a file to be uploaded. Sample:
element = driver.find_element_by_css_selector("input[type=file]")
element.send_keys("/absolute/path/to/a/file")
Also see related topics:
How to upload file ( picture ) with selenium, python
selenium webdriver upload file
Well, there are certainly other options, like using AutoIt or Sikuli, but they would really be either unreliable, or complex, slow, platform or browser dependent (or all of it).
Here is a sample working code:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://www.kijiji.ca/p-post-ad.html?categoryId=36")
driver.find_element_by_link_text("British Columbia").click()
driver.find_element_by_link_text("Cariboo Area").click()
driver.find_element_by_link_text("100 Mile House").click()
driver.find_element_by_id("LocUpdate").click()
driver.find_element_by_css_selector("#ImageUpload [type=file]").send_keys("/Users/user/Downloads/test.jpg")
Which produces:

Sitecore file download works only for admin user

I have a Sitecore 6.4 setup where an editor can click a button to generate a Word doc. I was adding the file to the media library and that was working fine. The editor would click the button in the content editor, the file was generated, media item was generated, then the content editor would show the new item in the media library and the editor could click the "download" button on the ribbon or the item to download it. However, my media library was getting unnecessarily filled up so I am trying to bypass the media library.
Instead of making the file in an arbitrary location as before, I am putting it in the temp directory like this:
wordOutputPath = Sitecore.IO.FileUtil.GetWorkFilename(Sitecore.Configuration.Settings.TempFolderPath, printItem.Name, ".docx");
File.Copy(wordTemplatePath, wordOutputPath);
WordprocessingDocument doc = WordprocessingDocument.Open(wordOutputPath, true);
After I "fill in" the file with content, I do this:
Sitecore.Context.ClientPage.ClientResponse.Download(wordFilePath);
Now if I am logged in as a Sitecore admin I get the browser's download dialog and can download the file. However, if I am logged in as a non-admin user, I get a little clicking and whirring, so to speak, and the file is generated, but the save file dialog never comes up in the browser. I can go in through the file system and see & open the Word doc, and it looks fine.
I found something in the Sitecore release notes for 6.6:
Released Sitecore CMS and DMS 6.6.0 rev. 130111 (6.6.0 Update-3)
[...]
Miscellaneous
Only Administrators were allowed to download files. (316774, 348557)
This was a problem in several areas of the system, for example the Package Generator and the Export Language Wizard in the CMS. It also affected the Export Users Wizard in the ECM module.
So I tried using SecurityDisabler (no longer have the code handy) and UserSwitcher like this:
using (new Sitecore.Security.Accounts.UserSwitcher(Sitecore.Security.Accounts.User.FromName("sitecore\admin", false)))
{
Sitecore.Context.ClientPage.ClientResponse.Download(wordFilePath);
}
The IUSR and IIS_IUSRS accounts both have read, list & read/execute permissions on the temp folder and the individual files show read & read/execute permissions for those two accounts as well.
What can I do to allow non-admin users to download these files? Does the bug that was fixed in 6.6 have anything to do with it?
Thank you.
Calling Sitecore.Context.ClientPage.ClientResponse.Download() method will result in a Download command sent back to the Sitecore Client in the Web browser, which in turn will make a call back to the server to execute it. This will be why using a security switcher (or a security disabler for that matter) before calling the Sitecore.Context.ClientPage.ClientResponse.Download() method has no effect, as the admin only restriction is being applied to the download else where.
If you search through sitecore\shell directory in your webroot, you will find that there is a file called download.aspx. This should be the actual page the download request is sent to. This web form page inherits the Sitecore.Shell.DownloadPage class, so if you take a look at that class's implementation, you will find that the OnLoad method is implemented as such:
protected override void OnLoad(EventArgs e)
{
Assert.ArgumentNotNull(e, "e");
base.OnLoad(e);
string fileHandle = StringUtil.GetString(new string[] { base.Request.QueryString["file"] });
bool flag = false;
string filename = FileHandle.GetFilename(fileHandle);
if (!string.IsNullOrEmpty(filename))
{
fileHandle = filename;
flag = true;
}
if (!string.IsNullOrEmpty(fileHandle))
{
if (MediaManager.IsMediaUrl(fileHandle))
{
this.DownloadMediaFile(fileHandle);
}
else if (fileHandle.IndexOf("id=", StringComparison.OrdinalIgnoreCase) >= 0)
{
this.DownloadMediaById(fileHandle);
}
else if (flag || Context.IsAdministrator)
{
this.DownloadFile(fileHandle);
}
}
}
As you can see, the last if block has an IsAdministrator check which would be where your non-admin users are getting blocked.
Although I haven't 100% verified that this is where the problem lies, the download restriction issue can be resolved, by creating a new class that inherits Sitecore.Shell.DownloadPage and overrides the OnLoad implementation with some extra security checks as required. I recommend that you don't remove the IsAdministrator check altogether as it is there for a good reason. Update the Inherits attribute in the download.aspx to make it use it instead.

Accessing Firefox tab element in nsIWebProgressListener::OnStateChange using C++

I am developing extension for Firefox 3.0-3.5 versions using VS2008.
I want to set attribute to a tab once the document load request completes within that tab window.
So in OnStateChange method, I am checking for document load.
I have used STATE_STOP & STATE_IS_DOCUMENT for it.
I want to determine which tab window has been associated with particular document request.
I have valid DOM Document pointer got from nsIWebProgress *aWebProgress which is 1st input
parameter of OnStateChange.
if ((aStateFlags & STATE_STOP) && (aStateFlags & STATE_IS_DOCUMENT))
{
nsCOMPtr<nsIDOMWindow> domwin;
nsCOMPtr<nsIDOMDocument> domDoc;
aWebProgress->GetDOMWindow(getter_AddRefs(domwin));
domwin->GetDocument(getter_AddRefs(domDoc));
}
I have tried to get nsIDOMDocumentXBL pointer by QIing nsIDOMDocument pointer(domDoc in my example) but it fails with Error code 0x80004002 (2147500034) i.e.NS_ERROR_NO_INTERFACE.
How do I get the tab element corresponding to document load request.
Could any one please help me?
Thanks in Advance,
Vaibhav D. Gade.
IF I understood the question correctly and you want a for a content window, you probably need https://developer.mozilla.org/en/Working_with_windows_in_chrome_code#Accessing_the_elements_of_the_top-level_document_from_a_child_window to get the chrome window, then run the implementation of gBrowser.getBrowserForDocument in the chrome window.
You'd save yourself a lot of time if you stopped writing C++ and switched to JS for such things.