sgrillon sgrillon - 1 year ago 54
HTML Question

How to click Onclick from <a without text Javascript form using Selenium?

This is my source code :

<a onclick="pd(event);" tabindex="0" issprite="true" data-ctl="Icon"

style="overflow: hidden; background: transparent

url("webwb/pygridicons_12892877635.png!!.png") no-repeat scroll 0px top; width:

16px; height: 16px;" name="ManageStakeholder_pyWorkPage_202"

title="Add a data " data-click="[["doGridAction",

[":event","INSERTAFTER"]],["refresh", ["thisSection","", "", "&=", "",

",",":event","","pyWorkPage"]]]" data-hover="[["changeStyle",

[":event","background-position:0px -16px;", "1"]]]"

onmouseover="pega.c.cbe.processBehavior(event, "data-hover")"></a>

I need click to image (pygridicons_12892877635.png). I using Selenium with chromewebdriver and I look at the selection OK but the onclick from "a" HTML balise do not detected the click.

I see this: How to click Onclick Javascript form using Selenium? The solution is click to the text
<a ...> text </a>
but in my case I do not have any text.

Answer Source

I find a solution by javascript execution (from java):

import org.openqa.selenium.JavascriptExecutor;

String xpath = "//table[@param_name='SubSectionManage~pzLayout_3']//a[@title='Add a data ']";

((JavascriptExecutor) webDriver).executeScript("document.evaluate(\"" + xpath + "\", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);");

In this example the xpath is the normal xpath. The important element is executeScript (in java) and document.evaluate (in javascript).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download