Imagine that you need to interact with an element on the page but do not know the ID or the name of the element. In Chapter 2, Locators, we saw a number of different XPath strategies so let us try to translate our test cases into Selenium 2.
Create a new Java class and create the
setUp()
andtearDown()
methods.The test needs to load http://book.theautomatedtester.co.uk/chapter2.
Find the element with the text Sibling Button.
Click on the button.
Run your test. Your code should appear as follows:
@Test public void shouldLoadChapter2AndClickOnSiblingButton(){ driver.get("http://book.theautomatedtester.co.uk"); WebElement sibling = driver.FindElement( By.xpath("//input[@value='Button with ID'] /following-sibling::input[@value='Sibling Button']" ) ); Sibling.click(); }