1

I need to find the button inside of this table. The ideal would be to find it by the 'Course Home' text because that's how users find it, but at this point, since that's not what I'm testing, I'm willing use any method as long as it can find and click on the button.

I have tried:

  • By.cssSelector("div.buttons:nth-child(1) > button:nth-child(1)") //least favorite method
  • By.partialLinkText("Course Home");//the ideal
  • By.cssSelector("div.two>div.buttons>button")// and other such attempts, just grasping at straws

but none of these work. My test just hangs, looking for the button until it hits the timeout. What am I doing wrong?
PS I'm using Java.

    <td valign="middle">
        <div class="two">
            <div class="buttons">
                <button onclick="document.location.href='file/path/redacted/'">
                    <img src="/images/button_icons/house.png">
                    Course Home
                </button>
            </div>
        </div>
     </td>
bad_coder
  • 11,289
  • 20
  • 44
  • 72
item5
  • 195
  • 3
  • 17

3 Answers3

1

Try using xpath as:

By.xpath("//button/img[.='Course Home']");

or

By.xpath("//button[contains(.,'Course Home')]");
Vivek Singh
  • 3,641
  • 2
  • 22
  • 27
1

I am a fan of text based search of xpath

//*[.='Course Home'] 

Just need to make sure the text includes all the leading or tailing spaces and that's pretty much it. With . it gives us the ability to directly point to the parent node of the element wheres * search any tag on the page

Saifur
  • 16,081
  • 6
  • 49
  • 73
  • Does using xpath mean I won't be able to run tests on IE? – item5 Jan 16 '15 at 01:05
  • No I use IE for my testing as well. It's little slow sometimes but works – Saifur Jan 16 '15 at 01:31
  • 2
    @user3352335 IE doesn't have native xpath engine, therefore using xpath on IE has performance implications and inconsistencies with how other browsers behave for certain xpaths – nilesh Jan 17 '15 at 15:41
0

I prefer CSS over xpath and my answer on that can be found here

As for your solution xpath options provided by Vivek and Saifur should work too. I will provide some css options

 div.buttons>button

or

 div.two>div.buttons>button

or

 div.two button
Community
  • 1
  • 1
nilesh
  • 14,131
  • 7
  • 65
  • 79