IUIAutomation.ElementFromPoint does not give precise value for browser controls - c#

I wrote a C# program to find the information about controls at a point. To do this, I call ElementFromPoint method of IUIAutomation. It gives IUIAutomationElement. From that I get properties of the element, such, as CurrentControlType, CurrentName, CurrentBoundingRectangle.
This works well in applications like MS Word, Notepad. But when I try to find information of an element in a browser, it only identifies the document, which is the client area of the browser. So, even if I try to get information about a button in the client area, it only identifies that it is the client area. It does not identify the button.
I tested in IE, Chrome and Firefox. In all these browsers, I am facing this problem.
Please let me know how to get precise information about the control at a point inside a browser.
If not UI automation, is there any way I can write a C# program to get the information about the control under mouse cursor in a browser the way "Inspect" of Chrome finds? Please note that the browser will not be running inside my application.

Related

C# Selenium how to interact with chrome extension

I have successfully loaded my chrome profile with selenium that has pre-installed extensions.
The extension in question is called Private Internet Access, but i have tried others and cannot communicate with them
I am trying to figure out how i can open the extension below and enter username&password and click login button. I have tried finding the elements in by id and class but it doesnt find them. Can someone point me down the right line for this. Thanks
I've not had the ability to try this - however I've done a lot of work with Selenium and I've created my own chrome extension.
That extension popup in the top right actually a different web page. You won't be able to interact with it due to how it's presented, but you can navigate to it.
Try this:
Open up your extension, right click anywhere and select Inspect. This will open up devtools for the popup.
In devtools, go to the console and type document.url. This will give you the URL of that popup page. (you'll also see it at the top of the title bar)
That gibberish-looking bit in the middle doesn't change - it's your chrome extension identifier. From when I created my extension and 18 months worth of updates (including migrating from chrome extension manifest v2 to v3) it has not changed for me. I think you'll be safe to use it.
Go to that URL to make sure it works. In my case it's chrome-extension://ojhcleddagaoaplflbafhpekcciikdop/popup.html
You'll notice you'll lose some of the page styling compared to the popup. When you design the popup you give it fixed dimensions - when you open it as a normal webpage, you lose that and everything gets stretched.
Finally - for your automation - put that chrome-extension:\\ url as the first step in your selenium script and do your sign in. Then, carry on with the rest of your test.

How can I remotely take an accurate screenshot of a webpage by URL without WebBrowser?

I tried WebBrowser but any solution related to WebBrowser I find uses the exact same codes, WebBrowser.DrawToBitmap
Problem with that is, it gives out inaccurate results depending on URL, to what page actually would look like, sometimes even blank pages.
So I am looking for any alternate solution to WebBrowser if there is any.
It needs to run in background, as in not open any browser on screen, render it in background with all scripts and get an image.
If I understand what you're trying to do, you might be interested in a framework called PhantomJS, which is a WebKit "browser" engine which runs the pages without visually rendering them. It can be used to capture screens.
Now this technique requires JavaScript, but there is something called Selenium WebDriver to help you wrapping that. Users here at SO posted a simple example and this comment looks useful as it contains the list of required Packages.

how can i use IE (or different browser) for each webbrowser controle?

i want to use different browsers for my webbrowser controls in c#.NET.
for example, if im log into facebook from the webbrowser1, i will not in the webbrowser2
if i cant use ie different for each webbrowser... can i separate cookies for each webbrowser control?
for example, if i navigate using the first controle
webbrowser1.navigate("this is not important");
then submit the login form
webbrowser1.document.getelem..... click
thats will not applicated on the secod control( webbrowser2)
-
i hope you inderstand what i want :)
The WebBrowser control is a wrapper around IE and will always and only use IE. It's never going to use Firefox or Chrome etc. Some browsers apart from IE may allow Automation, but you'll have to check their documentation.
In your two lines of code, both are using the control called webbrowser1, so the Navigate and getelement will always go to the control. If you want to use two browser instances, you need to have 2 controls.
I don't clearly understand what your question is. The two points above are what I can answer.

Simulating mouse movement, without browser "head", to web server

I need to connect to a website programmatically, but without rendering the browser view, and then query the "headless" browser for certain page element's coordinate positions and send JavaScript events to simulate the movement of a mouse across the page form its current location to the new location.
I already have found a method that "realistically" moves the mouse programmatically between two points. All I need to do now is find a library that will allow me to connect to a page without the GUI but includes the positions of the page elements, but still query the JavaScript of the page and send JavaScript to the page's browser instance.
I know this very specific, but it will allow us to simulate our pages and test PHP heat mapping of the mouse location. It is also vital that this is for C#. Does such a library exist for C#?
Have you looked at Selenium? This can be run in headless mode, and has a very powerful API. I've used in C# for automated testing (including Javascript manipulation), and it works very well. It should be fine for scraping too though.

Moving Picture Box depending on monitor size? C#

I am using a picture box in my C# application, I want it to cover a certain portion of the web browser (the username part on youtube).
I have a 21.5" monitor and this is what it looks like to me:
But then this is what it looks like to one of my users with a 24" monitor:
As you can see the position of the picture box has moved up due to that persons screen size (I believe)
Is there a way to make sure that it will always be over that section of the web browser or moving it to that section of the web browser?
Thanks.
I am convinced your approach is wrong and would break anytime either for screen resolution or size changes, or for using the mouse-wheel to zoom in/out the page or whatever. it is just unreliable and patching this by overlapping another UI control like a picture box or a panel on top of what you want to hide is simply insecure and unreliable.
I think the tow real options you have are these:
You try to interpret the page content and remove from the page's DOM the information you do not want to show to the user (eventually HTML Agility Pack could help for this DOM parsing and manipulation but I am not sure if you can read what the WebBrowser control is showing and inject changes into it);
use the YouTube APIs and Tools - .NET APIs to load the videos and details you want to load and show but rendering this information with your specific UI elements in your windows forms application, without using a browser to show the normal YouTube site.
Probably the second option takes more work but is more secure, I am not sure 100%, as I said, if the first option is viable at all. You could search for HTML Agility Pack and web browser control to see if anybody has done this before already :)

Categories

Resources