Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium click on coordinates not clicking where expected

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.

like image 723
greenwaterboy Avatar asked Aug 19 '14 00:08

greenwaterboy


2 Answers

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();
like image 75
Erti-Chris Eelmaa Avatar answered Oct 15 '22 15:10

Erti-Chris Eelmaa


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();
like image 44
mutt Avatar answered Oct 15 '22 15:10

mutt