如何使用Selenium从<a没有文本的Javascript表单中单击Onclick?



这是我的源代码:

<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>

我需要点击图像(pygridicons_12892877635.png)。我使用Selenium与chromewebdriver和我看选择OK,但从"a"HTML balise的onclick没有检测到点击。

我看到这个:如何使用Selenium单击Onclick Javascript表单?解决方案是单击文本<a ...> text </a>,但在我的情况下,我没有任何文本。

我找到了一个解决方案,javascript执行(从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).singleNodeValue.click();");

在本例中,xpath是普通的xpath。重要的元素是executeScript (java)和文档。(在javascript中).

最新更新