Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium: How to verify menu drop down text?

I'd like to be able to verify the menu has all the proper dropdown menus with out clicking/selecting, just verifying the id/string of each menu item is ok, I saw from here Selenium: How to select an option from a select menu? how I can select them, but I don't want to select them. Thanks for any help.

like image 700
Green Avatar asked Feb 15 '26 06:02

Green


1 Answers

I think you could do something like this to verify the element on the page without selecting them, you're xpath would probably vary my example is quite simplified:

HTML:

<body>

<select>
  <option>One</option>
  <option>Two</option>
  <option>Three</option>
  <option>Four</option>
</select>
</body>

Selenium Test Case:

public class HomePageTest {

public static HtmlUnitDriver driver;

@Before
public void setUp() throws Exception {
   driver = new HtmlUnitDriver();
}

@Test
public void initiateTest() throws Exception {
      driver.get("http://localhost/test3.html");
      List<WebElement> elems = driver.findElementsByXPath("//option");
      for (WebElement e : elems)
      {
          System.out.println(e.getText());
      }
}

@After
public void tearDown() throws Exception {
driver.close();
} }
like image 159
Mike K. Avatar answered Feb 16 '26 19:02

Mike K.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!