\DMore\ChromeDriver\ChromeDriver::click can miss the element
Hello!
Currently, click attempts to click in the center of the element. I found that if the element is wrapping to the next line then the click method can miss clicking on the element and click on the surrounding element instead.
I hacked the page with some JS to record the click and the JS replaced the <h1> content with the coordinates and the element tag name that was clicked (Hope that makes sense, it's the 1 Y: 462 X: 110 TAG: TD in the image). In the image, it should be clicking on "Test cohort name" but it clicks on the table TD tag.
Not sure of the best solution, but this seemed to work. Instead of clicking in the center, click 10px (at the most) in from the top left. The click coordinates could be less than 10px from top left if the element is very small.
NOTE: Not sure why the first line, $this->mouseOver($xpath); is there, wondering if it could be removed?
public function click($xpath)
{
$this->mouseOver($xpath);
if ($this->runScriptOnXpathElement($xpath, 'element.tagName == "OPTION"')) {
$this->setValue($xpath . '/..', $this->runScriptOnXpathElement($xpath, 'element.value'));
} else {
list($left, $top, $width, $height) = $this->getCoordinatesForXpath($xpath);
// Move in at most, 10 pixels from the top left of the element.
for ($i = 10; $i != 0; $i--) {
if ($i < $width) {
$left += $i;
break;
}
}
for ($i = 10; $i != 0; $i--) {
if ($i < $height) {
$top += $i;
break;
}
}
$this->page->send('Input.dispatchMouseEvent', ['type' => 'mouseMoved', 'x' => $left, 'y' => $top]);
$parameters = [
'type' => 'mousePressed',
'x' => $left,
'y' => $top,
'button' => 'left',
'timestamp' => time(),
'clickCount' => 1,
];
$this->page->send('Input.dispatchMouseEvent', $parameters);
$parameters = [
'type' => 'mouseReleased',
'x' => $left,
'y' => $top,
'button' => 'left',
'timestamp' => time(),
'clickCount' => 1,
];
$this->page->send('Input.dispatchMouseEvent', $parameters);
usleep(5000);
$this->waitForDom();
}
}
If you like this change or think of a better one, I can send a PR. Just wanted to submit this before the weekend.
Cheers! Mark
PS: If you haven't figured it out already, I'm coming from the Moodle project, https://tracker.moodle.org/browse/MDL-58948
