How to locate a list element (Selenium)? How to locate a list element (Selenium)? selenium selenium

How to locate a list element (Selenium)?


I think that the XPath should be "//ul/li[1]". In selenium the first item is 1, not 0. Look here


I know this is not as efficient as the other answer but I think it gives you the result.

WebElement element = (WebElement) ((JavascriptExecutor)driver).executeScript("return $('li').first()");String item = element.getText()


(//ul/li)[1]

This selects the first in the XML document li element that is a child of a ul element.

Do note that the expression:

//ul/li[1]

selects any li element that is the first child of its ul parent. Thus this expression in general may select more than one element.