简体   繁体   中英

how to click on links on images using selenium java

I am unable to click the link present in image-->map--> area tag. Can any one help me how to solve this. I want to click on no (or) close.

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

The following code may works.

Method 1:

WebElement close=driver.findElement(By.xpath("//*[@id='IPEinvL']/map/area[@alt='close']"));

JavascriptExecutor js = (JavascriptExecutor) driver;  
js.executeScript("arguments[0].click();",close);

Method 2:

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

Method 3: use Actions class

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

Java click() must work as follows:

  • To click No :

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM