9

I need to screen scrape a webpage that is using ActiveX controls for the navigation. This is not for ui testing purposes, its for data downloads from a legacy application.

The issue I have is the top navigation is complete ActiveX with javascript and is impossible to get the elements by anything. So I am trying to do mouse clicks at the coordinates.

I am using the following method answer by Bergstrom

Basically I am doing

var action = new Actions(ieDriver).MoveToElement(ieDriver.FindElement(By.Tag("HTML"))).MoveByOffset(200,100).Click().Perform();

I confirmed while debugging that ieDriver.FindElement returns -1,-1 for the location of the HTML tag, so the offset coordinates should be correct.

I measured the coordinates using IE Toolbar. When I run the code nothing happens, so I assume its click in blank space.

Is there a way to ping the browser so I know where the coordinates are or is there a better way of achieving this?

I was able to successfully do this using VS Coded Unit Test since it actually moves the cursor, but I don't think the licensing will allow me to use that option as well as the annoyance of getting it to run outside of visual studio.

Community
  • 1
  • 1
greenwaterboy
  • 95
  • 1
  • 1
  • 5
  • Separate your actions out with a `.perform()` and a wait in between them. Then you can see where it is actually moving the mouse to instead of it doing it all at once. – mutt Aug 19 '14 at 13:17
  • On another note though, why are you clicking a point? If the resolution changes at all on your browser this step won't work cause the point it is clicking will be wrong. Not very adaptable for web applications that change based on users monitors... – mutt Aug 19 '14 at 13:18

2 Answers2

6

I spent like 2 hours trying to get it to work, I had an element inside iframe,

keep in mind there's a lot of code examples in internet that don't work, I eventually settled to this one which actually works exactly as it has to:

_driver.SwitchTo().Frame(recaptchaChallengeFrame);

var body = _driver.FindElement(By.XPath(".//body"));

Actions builder = new Actions(_driver);

builder
    .MoveToElement(body, absClickX, absClickY)
    .Click()
    .Build()
    .Perform();
Erti-Chris Eelmaa
  • 25,338
  • 6
  • 61
  • 78
  • you saved my day. I was looking for solution to my problem. I was facing issue to Find Element by Id or anything else. This issue was due to Frame I was looking at. Your solution switched on tube-light in my mind. I changed the Frame and I got my element. – Ashok Feb 21 '17 at 09:55
5

Rather than trying to get an element just move by offset. Make sure you know what your prior focus is... If none then it should be the top left corner of the page. Then put your sleep in the middle and you should be able to see the mouse move, wait, and then click.

Actions action = new Actions(driver);
action.MoveByOffset(200,100).Perform();
Thread.Sleep(10000);
action.Click();
mutt
  • 783
  • 1
  • 7
  • 11
  • That worked when I tried it on google and other sites. Didn't work with my target site, I think its because the site has several layers of frames. Decided to go with using the winapi to do mouse clicks since the buttons are always in the same position. – greenwaterboy Aug 19 '14 at 15:48
  • how did you use the winapi? – Erti-Chris Eelmaa Oct 08 '16 at 17:50