一尘不染

单击使用Selenium 3 Webdriver的图像映射中的特定项目

selenium

我正在使用Selenium 3.3.1和Java Webdriver绑定,并且需要单击页面上图像映射中的特定项。这是图像地图的HTML

<img id="menu1" name="menu1" src="../../images/images/menu23t.gif" width="950" height="68" border="0" usemap="#Map">

<map name="Map">
    <area id="manager_indx" shape="rect" coords="220,13,297,60" href="../../backend/manager/index.php" target="_parent">
    <area id="pos" shape="rect" coords="306,13,336,60" href="http://10.10.10.99:8080/frontend/index.html" target="_parent">
    <area id="end_of_day" shape="rect" coords="347,13,410,60" href="../../backend/manager/end_of_day.php" target="_parent">
    <area id="customer" shape="rect" coords="415,10,470,60" href="../../backend/managecustomers/index.php" target="_parent">
    <area id="employee" shape="rect" coords="477,12,537,60" href="../../backend/employee_f/index.php" target="_parent">
    <area id="reports" shape="rect" coords="540,12,590,61" href="#" onclick="chk_report_security()">
    <area id="cash" shape="rect" coords="596,13,640,60" href="../../backend/manage_register/index.php" target="_parent">
    <area id="inventory" shape="rect" coords="650,12,705,60" href="../../backend/inventory/index.php" target="_parent">
    <area id="configuration" shape="rect" coords="720,11,802,60" href="../../backend/generalsetup/configuration.php" target="_parent">
    <area id="logon" shape="rect" coords="815,11,848,60" href="http://10.10.10.99:8080/frontend/index.html" target="_parent">
    <area id="exit" shape="rect" coords="852,11,890,61" href="javascript:javascript:exit_logout();">
    <area id="help" shape="rect" coords="892,14,937,60" href="javascript:top.banner.openContainer(window.parent.banner.isThrive);" target="_parent">
</map>

这是我尝试使它单击ID为“报告”的区域的内容。

WebElement banner = driver.findElement(By.cssSelector("map"));
WebElement area = banner.findElement(By.id("reports"));
area.click();//This click isn't working

我也尝试过ID,cssSelector和xpath

driver.findElement(By.id("reports")).click();

driver.findElement(By.cssSelector("#reports")).click();

driver.findElement(By.xpath("//*[@id=\"reports\"]")).click();

还有其他xpath变体,例如// area [@ id =’reports’]。

我主要使用Firefox和Geckodriver
0.15.0,但是它也在其他浏览器中发生。早在2011年就为此打开了一个错误,但该错误仅被确定为chrome,而现在该线程已经很老了,基本上没有用。如果有人最近做过这件事,那么如果他们可以分享自己的做事方式将大有帮助。谢谢!


阅读 258

收藏
2020-06-26

共1个答案

一尘不染

回答我自己的问题,因为答案来自评论中的建议。答案是使用JavaScript执行器。就像是:

WebElement banner = driver.findElement(By.cssSelector("map"));
WebElement area = banner.findElement(By.id("reports"));

JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", area);
2020-06-26