0
votes

I am new to testing and need some help on Xpath for following code :

snapshot of the elements

InnerHTML of the element:

<img class="icon" src="custom/icons/Injury.png">
<span class="buttonText"><span class="title">An injury</span></span>
<span class="title">An injury</span>

I want to click on "An injury", have tried below xpath but its not working

driver.findElement(By.xpath(".//span[text()='An injury']")).click();

    <div id="pageLayout-eLeType-AFBWtFlqgByDxQjVusTPqEu0-bMs-AFBWtFlqgByDxQjVusTPqEuw" class="formCell button80Percent" style="width:48.0%;"> 
     <div class="dashboardLinksContainer">
      <div title="" class="dashboardLink linkAsButton">
       <a href="/QUASAR/new/Incident" onclick="userFavourites.recordVisit('Incident', 'anVtcENvbW1hbmQoJz9jb21tYW5kPW5ldyZicGlkPUFCTnlsR0dYTWpRMk9EazJNREkwTXpNNCZ0aXRsZT0iSW5jaWRlbnQiJyk7');jumpCommand('?command=new&amp;bpid=ABNylGGXMjQ2ODk2MDI0MzM4&amp;title=&quot;Incident&quot;');return false;"><img class="icon" src="custom/icons/Injury.png">
         <span class="buttonText">
           <span class="title">An injury</span>
         </span>
       </a>
      </div>
     </div>
   <div class="clear"></div>
 </div>

<div id="pageLayout-eLeType-AFBWtFlqgByDxQjVusTPqEu3-bMs-AFBWtFlqgByDxQjVusTPqEuw" class="formCell button80Percent" style="width:48.0%;"> 
  <div class="dashboardLinksContainer">
    <div title="" class="dashboardLink linkAsButton">
      <a href="/QUASAR/new/Incident" onclick="userFavourites.recordVisit('Incident', 'anVtcENvbW1hbmQoJz9jb21tYW5kPW5ldyZicGlkPUFCTnlsR0dYTWpRMk9EazJNREkwTXpNNCZ0aXRsZT0iSW5jaWRlbnQiJyk7');jumpCommand('?command=new&amp;bpid=ABNylGGXMjQ2ODk2MDI0MzM4&amp;title=&quot;Incident&quot;');return false;">
         <img class="icon" src="custom/icons/Damage.png">
      <span class="buttonText"><span class="title">Damage or loss to property or asset</span>
     </span>
    </a>
   </div>
  </div>
 <div class="clear"></div>
</div>

Here is the 2 html code and there are about 8 more and all have same span class >buttonText and Span class = title with just different title names

2
Can you please check, whether that particular element resides in iframe tag? - Subburaj
Why did you add xpath-2.0 tag? Selenium doesn't support XPath 2.0. Also let us know which element exactly you want to click: provided HTML sample differs from HTML on attached picture - Andersson
I have provided html code and I want to click on title>An injury - Farha
Did you try to click the <a> tag instead of the <span> tag? There's an inline JavaScript registered to the <a> tag onclick event. - Mincong Huang

2 Answers

0
votes

As per the HTML you have shared to invoke click() on the element with text as An injury you can use either of the following solutions:

  • cssSelector:

    driver.findElement(By.cssSelector("div.dashboardLink.linkAsButton > a[href='/QUASAR/new/Incident'] span.buttonText > span.title")).click();
    
  • xpath:

    driver.findElement(By.xpath("//div[@class='dashboardLink linkAsButton']/a[@href='/QUASAR/new/Incident']//span[@class='buttonText']/span[@class='title'][contains(.,'An injury')]")).click();
    
0
votes

You can use CSS selector

span.title:nth-child(3)

Edit You can use below Xpath

".//*[@id='pageLayout-eLeType-AFBWtFlqgByDxQjVusTPqEu0-bMs-AFBWtFlqgByDxQjVusTPqEuw']/div[1]/div/a/span/span"

or Use Link text

"An injury"