【问题标题】:how to click on links on images using selenium java如何使用 selenium java 单击图像上的链接
【发布时间】:2017-09-14 12:31:59
【问题描述】:

我无法点击图片-->地图-->区域标签中的链接。谁能帮我解决这个问题。我想点击不(或)关闭。

<div id="IPEinvL" style="z-index: 10000; width: 439px; height: 360px; left: 463px; top: 0px; background-color: white; position: absolute; margin-left: 0px; margin-top: 0px;">
    <img alt="Would you like to participate in a short study?" usemap="#IPEMap" src="XXXXXXXXXXXXXXXXXXXX.png" border="0" height="360" width="439">
       <map name="IPEMap">
           <area shape="rect" coords="405,15,424,33" href="javascript:clWin()" alt="close">
           <area shape="rect" coords="117,229,214,258" href="javascript:fOpen()" alt="yes">
           <area shape="rect" coords="225,229,323,258" href="javascript:clWin()" alt="no">
       </map>
    <img id="countInvites" src="XXXXXXXXXXXXXXXXX/Counter/counter_N.png?surveyID=120799&amp;siteID=1&amp;langID=1&amp;traceID=2" style="border: 0px; margin-top: -10px;" alt="" height="0" width="0">
</div>

【问题讨论】:

    标签: javascript java selenium selenium-webdriver


    【解决方案1】:

    以下代码可能有效。

    方法一:

    WebElement close=driver.findElement(By.xpath("//*[@id='IPEinvL']/map/area[@alt='close']"));
    
    JavascriptExecutor js = (JavascriptExecutor) driver;  
    js.executeScript("arguments[0].click();",close);
    

    方法二:

    WebElement close=driver.findElement(By.xpath("//*[@id='IPEinvL']/map/area[@alt='close']"));
    String hrefvalue=close.getAttribute("href");
    JavascriptExecutor js = (JavascriptExecutor) driver;  
    js.executeScript(hrefvalue);
    

    方法三: 使用 Actions 类

      new Actions(driver).click(close).build().perform();
    

    【讨论】:

    • 我已经尝试了 3 种方法得到低于异常 org.openqa.selenium.NoSuchElementException: Unable to locate element: //*[@id='IPEinvL']/img/map/area[@alt= '关闭']
    • 抱歉 xpath 错误,应该是 //*[@id='IPEinvL']/map/area[@alt='close']。请检查更新的 xpath
    • Xpath //*[@id='IPEinvL']/map/area[@alt='close']。为我工作。它拯救了我的一天。谢谢!
    • 我可以知道为什么第一个 xpath 不起作用。我尝试了很多 xpath 的方法,但没有奏效。
    • 因为map标签不是img标签的父标签。
    【解决方案2】:

    Java click() 必须按如下方式工作:

    • 点击No

      driver.findElement(By.xpath("//map[@name='IPEMap']/area[@alt='no']")).click();
      
    • 点击Close

      driver.findElement(By.xpath("//map[@name='IPEMap']/area[@alt='close']")).click();
      

    【讨论】:

      猜你喜欢
      • 2017-12-26
      • 1970-01-01
      • 2014-06-10
      • 1970-01-01
      • 2017-12-25
      • 1970-01-01
      • 2015-08-18
      • 2020-09-23
      • 2020-07-14
      相关资源
      最近更新 更多