Search code examples
htmlrubywatir

How to click on link element after i element with specific text using watir?


For this type of link I am happily using

@browser.link(:text => "Reports").when_present.click

<a href="/Reports/">Reports</a>

However I am struggling with this example

<a href="#">
  <i class="no-icon"></i>
  Staff
  <img src="/Images/Menu/arrow.gif" class="arrow">
</a>

I am wanting to click the link by text also but it is not found due to i element. I do not want to use xpath so can anyone advise a better way please.

Thanks


Solution

  • The given below code works for me fine.

    browser.link(text: "Staff").click
    

    If it's not working, perhaps you may try by this

    browser.element(link: "Staff").click
    

    If it's still not working then write the following code

    browser.element(xpath: "//img[@src='/Images/Menu/arrow.gif']/..").click
    

    Remember when_present method is not necessary because that's automatic. WATIR automatically waits for button/link to exist, present, enabled.