'How do I find this button with Selenium? I tried partiallinktext and multiple attempts at cssSelector

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>


Solution 1:[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

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Saifur
Solution 2 Community