0

So my question is: I got an Windows Forms application in C# and an webBrowser control in this application. When you for example right-click on a video in youtube, a context menu shows up. Now, is it possible to programmatically rightclick in the webBrowser control an then, again programmatically, click a specific entry in that context menu?

burning_LEGION
  • 13,246
  • 8
  • 40
  • 52
VGD
  • 436
  • 1
  • 5
  • 25
  • Usually not. This tends to be a window that's owned by another process or is buried in a hierarchy of other windows. No good way to get the window handle. Have a look with Spy++. – Hans Passant Aug 10 '12 at 13:53

2 Answers2

0

Yes it is, but you always have to start from the same pixel, or better said an actual pixel range, so you can be sure that the clicked result will be the required one. Also you cant click an item by specifying its text, you must do everything programatticaly, from the graphics point of view(just work on the X - Y Axis since its only 2 dimensional). That is the way most web bots are made for various purposes.

Freeman
  • 5,691
  • 3
  • 29
  • 41
  • Well but that isn't really safe, is it? Let's say somebody has a different screen resolution, and the mouse click is not exact, the entry in the context menu won't be clicked. – VGD Aug 10 '12 at 13:10
  • you are correct. the solution for that would be to have instances working for a number of predefined scenarios. – Freeman Aug 10 '12 at 13:13
  • Better off by using a simple image recognition to find the exact positions without hardcoding it. – Viezevingertjes Aug 10 '12 at 13:26
  • yes but the accuracy it offers is not that astonishing and you might find yourself in a situation where your app clicked something else. – Freeman Aug 10 '12 at 13:29
0

Do you really have to simulate a click of the context menu or is just having the desired action good enough? If so than you can just get the item from the ContextMenu.Items list and assuming it a button raise its Click event. If you do need to at least show the context menu while do this you can call the ContextMenu.Show event. This all assumes that the contextmenu for your WebBrowser control is public (not some third party inherited control that hides it or something).

Mike
  • 418
  • 3
  • 10
  • I tried, problem is that webBrowser.ContextMenu always returns _null_ , then i just tried to disable the context menu, which worked but only for the websites where the context menu is the default webbrowser context menu. So I think the fact that youtube uses it's own context menu makes it not possible to access the context menu via webBrowser.ContextMenu. If you know how to access it though, tell me ;) – VGD Aug 11 '12 at 08:48