Locating li element inside ul element - selenium

i would like to get the count of li elements created inside ul element with a particular id. Below is the structure
<ul id="someid">
<li class="someclass"></li>
<li class="someclass"></li>
<li class="someclass"></li>
<li class="someclass"></li>
</ul>
Can someone help me locate li element with xpath.thanks.

You can use the following xpath
//ul[#id="someid"]/li[#class="someclass"]
If you want to filter the li elements you can give the restrictions based on attributes of the tag. Hope this helps.

To get the count of <li> elements created inside the <ul> element with a particular id you can use the following xpath :
driver.findElements(By.xpath("//ul[#id='someid']/li[#class='someclass']"))

Try the Below code to get count of <li> elements
List<WebElement> count=driver.findElements(By.Xpath(" //ul[#id="someid"]/li[#class="someclass"]"));
count.size()

Related

findElements(By.xpath()) finds elements only when given full path

I've a page containing the path
/html/body/div[5]/div[2]/div[2]/div/div[2]/form/div/div[2]/div/div/div[6]/div
of which the bottom-most div resolves to
<div id="tab-topTabs--Lumber">
<div>
<ul>
<li>
2x4
</li>
<li>
4x4
</li>
<li>
4x6
</li>
</ul>
</div>
</div>
both of these will find the right element
WebElement divTopTabLumber_1 = m.getDriver().findElement(By.xpath("/html/body/div[5]/div[2]/div[2]/div/div[2]/form/div/div[2]/div/div/div[6]/div"));
WebElement divTopTabLumber_2 = m.getDriver().findElement(By.id("tab-topTabs--Lumber"));
Problem - calling using the full path grabs the correct three list elements under the deepest div:
List<WebElement> list_lumber_0 = m.getDriver().findElements(By.xpath("/html/body/div[5]/div[2]/div[2]/div/div[2]/form/div/div[2]/div/div/div[6]/div/div/ul/li"));
but these do not:
List<WebElement> list_lumber_1 = divTopTabLumber_1.findElements(By.xpath("//div//ul//li"));
List<WebElement> list_lumber_2 = divTopTabLumber_2.findElements(By.xpath("//div//ul//li"));
reporting 206 elements instead of the expected 3.
Any suggestions/critiques/solutions would be greatly appreciated.
TIA,
Still-learning Steve
I would believe that this id
tab-topTabs--Lumber
is unique in HTMLDOM.
Steps to check:
Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the //div[#id='tab-topTabs--Lumber'] and see, if your desired element is getting highlighted with 1/1 matching node.
if it is then use the below code to locate all 3 li nodes:
List<WebElement> list_lumber_2 = divTopTabLumber_2.findElements(By.xpath(".//descendant::li"));

How to get count of 'li' elements list with selenium webdriver?

I have this html. I am trying to count total number of li elements inside the ul element. Even though there are 20 li elements but DOM shows less than 20.
<ul class="a-pagination">
<li class="a-disabled">←<span class="a-letter-space"></span><span class="a-letter-space"></span>Previous</li>
<li class="a-selected">1</li>
<li class="a-normal">2</li>
<li class="a-normal">3</li>
<li class="a-disabled" aria-disabled="true">...</li>
<li class="a-disabled" aria-disabled="true">20</li>
<li class="a-last">Next<span class="a-letter-space"></span><span class="a-letter-space"></span>→</li>
</ul>
Selenium should count the li elements inside the ul class because there are other li elements outside the ul class.
I have this:
List<WebElement> list1 = driver.findElements("not sure what will go here");
System.out.println("\nsize" + list1.size() );
What should be the locator to find the total li elements under the ul class? It should return the value of 20.
Can you try with this css selector :
ul.a-pagination li
like this :
List<WebElement> list1 = driver.findElements(By.cssSelector("ul.a-pagination li"));
and print like the way you are printing.

Identifying the Web element with same class name in Selenium

I have tried to get the number of tweets(tweet count) through selenium
Here is the page source:
<li class="DashboardProfileCard-stat Arrange-sizeFit">
<a class="DashboardProfileCard-statLink u-textUserColor u-linkClean u-block"
title="1 Tweet"
href="/saisiva14"
data-element-term="tweet_stats">
<span class="DashboardProfileCard-statLabel u-block">Tweets</span>
<span class="DashboardProfileCard-statValue" data-is-compact="false">1</span>
</a>
</li>
<li class="DashboardProfileCard-stat Arrange-sizeFit">
<a class="DashboardProfileCard-statLink u-textUserColor u-linkClean u-block"
title="38 Following"
href="/following"
data-element-term="follower_stats">
<span class="DashboardProfileCard-statLabel u-block">Following</span>
<span class="DashboardProfileCard-statValue" data-is-compact="false">38</span>
</a>
</li>
<li class="DashboardProfileCard-stat Arrange-sizeFit">
<a class="DashboardProfileCard-statLink u-textUserColor u-linkClean u-block"
title="4 Followers"
href="/followers"
data-element-term="following_stats">
<span class="DashboardProfileCard-statLabel u-block">Followers</span>
<span class="DashboardProfileCard-statValue" data-is-compact="false">4</span>
</a>
</li>
I could not able to locate the web element for getting Tweets,Followers & following. The reason is span class names are common for all these elements.Please help me .
To get number of Tweets/ Following/ Followers, You can try the below statements:
System.out.println(driver.findElement(By.xpath("//a[contains(#title, 'Tweet')]/span[2]")).getText());
System.out.println(driver.findElement(By.xpath("//a[contains(#title, 'Following')]/span[2]")).getText());
System.out.println(driver.findElement(By.xpath("//a[contains(#title, 'Followers')]/span[2]")).getText());
To click on the Tweets/ Following/ Followers links, You can try the below statements:
driver.findElement(By.xpath("//a[contains(#title, 'Tweet')]")).click();
driver.findElement(By.xpath("//a[contains(#title, 'Following')]")).click();
driver.findElement(By.xpath("//a[contains(#title, 'Followers')]")).click();
or
driver.findElement(By.xpath("//a[./span[text()='Tweets']]")).click();
driver.findElement(By.xpath("//a[./span[text()='Following']]")).click();
driver.findElement(By.xpath("//a[./span[text()='Followers']]")).click();
The above statements are working fine for me.
try this
IList<IWebElement> elements = driver.FindElements(By.ClassName("DashboardProfileCard-stat"));
foreach (IWebElement element in elements)
{
IWebElement ele = element.FindElement(By.ClassName("DashboardProfileCard-statLabel"));
if (ele.Text == "Tweets")
{
return element.FindElement(By.ClassName("DashboardProfileCard-statValue")).Text;
}
}
this is using C#, you can modify accordingly if anyother language is used.
The selector .DashboardProfileCard-stat span:nth-child(2) should give you the collection of web elements pointing to the count. For example in Java:
ArrayList<WebElement> elements = driver.findElements(By.cssSelector(".DashboardProfileCard-stat span:nth-child(2)"))
Then you can use elements.get(0).getText() for tweets. elements.get(1).getText() for following. elements.get(2).getText() for followers. So:
ArrayList<WebElement> elements = driver.findElements(By.cssSelector(".DashboardProfileCard-stat span:nth-child(2)"));
int tweets = elements.get(0).getText();
int following = elements.get(1).getText();
int followers = elements.get(2).getText();
Of course, do your appropriate safety checks, etc. Check the length of the array before access.
This code is in Java :)
capture all the parents of the "SPAN" into a collection item.
Iterate on the collection to find the span elements (which are child of <a> tag) and capture text based on the class name variation statLabel / statValue".
List<WebElement> webElement = driver.findElements(By.xpath("//li[#class='DashboardProfileCard-stat Arrange-sizeFit']//a"));
for (WebElement element : webElement) {
System.out.println(element.findElement(By.xpath("//span[contains(#class,'statLabel')]")).getText());
System.out.println(element.findElement(By.xpath("//span[contains(#class,'statValue')]")).getText());
}

Trouble on selecting auto-suggest and moving it into the textbox

Note:- Here, the Listitems are not under div class
Could able to locate the element but the select function is not working.
HTML used:
<head>
<body id="data-search" class="hassidebar">
‌<ul id="material-result-list" style="top: 183px; left: 396.5px; width: 270px; display: block;">
<li>
<li>
<li>
<a>nitrate/0.2</a>
</li>
<li>
<li>
</ul>
CODE used:
Try 1:
List<WebElement> listItems = driver.findElements(By.xpath("//ul[contains(#id,'material-result-list')]/li"));
listItems.get(2).click();
Try 2:
List<WebElement> listItems = driver.findElement(By.id("material-result-list")).findElements(By.tagName("li"));
listItems.get(2).click();
(used more combinations, please help on this)
I could retrieve the auto-suggest texts using getText() method [so it confirms that there are no issues on locating the element]
But has trouble on select them and placing under text field for search
List<WebElement> link = driver.findElements(By.xpath("//ul[contains(#id,'material-result-list')]/li"));
String secondoption = link.get(2).getText();
System.out.println(secondoption);
The above script is trying to click on tag "li" and not on anchor tag "a";
Here, the getText() method of tag "li" will return text.
So, we need to click on specific anchor tag.
Solution:
List<WebElement> listItems = driver.findElement(By.id("material-result-list")).findElements(By.tagName("a"));
listItems.get(2).click();

Robot Framework selecting from UL ID and li class

I am unable to select any of the ul id / li class items. I do not see a method for handling this. Anyone able to get it done?
<ul id="game_list">
<li class="game_link" onclick="update_blurb('one');">ONE</li>
<li class="game_link" onclick="update_blurb('two');">TWO</li>
<li class="game_link" onclick="update_blurb('three');">THREE</li>
<li class="game_link" onclick="update_blurb('four');">FOUR</li>
<li class="game_link" onclick="update_blurb('five');">FIVE</li>
<li class="game_link" onclick="update_blurb('six');">SIX</li>
</ul>
I am attempting to select the li class links.
I'm still not exactly sure what is the problem. The right method to click on things is Click Element. You can select any of the <li> elements by a CSS selector or an XPath expression (documentation, see "Locating elements").
For example:
Click Element | css=#game_list > li:nth-child(3)
clicks the third <li> element.
Or by text:
Click Element | xpath=id('game_list')/li[text()='THREE']
selects the <li> element whose text is "THREE".
Figured this out.
I create my own element id and click that new id.
Example:
Assign Id To Element xpath=//li[#onclick="update_blurb('one');"] one
Click Element one
Thanks for checking it out, Slanec.