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.
Related
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"));
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()
<div class="col-md-3 col-sm-3">
<h4>Free Resources</h4>
<ul>
<li>
NCERT Solutions for Class 10 Maths
</li>
<li>
NCERT Solutions for Class 9 Maths
</li>
<li>
NCERT Solutions for Class 10 Science
</li>
<li>
NCERT Solutions for Class 9 Science
</li>
<!-- CBSE Previous Year Paper -->
<li>
CBSE Previous Year Paper
</li>
<!-- RD Sharma solutions-->
<li>
RD Sharma Class 10 Solutions
</li>
<!-- Tutors SEO Pages-->
<li>
Home Tuition
</li>
<li>
Math Teacher
</li>
<li>
Science Teacher
</li>
</ul>
</div>
I have pasted the html I am working with. Just to make things more clear there exists two more div tags in the same hierarchy with the class name = 'col-md-3 col-sm-3'.
I found this question helpful (How to get all "li" elements of "ul" class in Selenium WebDriver) but I wanted to know how to do the same using PageFactory ?.
Please help me here. I am thinking that I will find the root of the list <div> element (using #FindBy annotation) and do the looping for each <a> tag and return this list from the Element repository class for that web page. Is there a better way to do this? I specifically want PageFactory version of this.
You can use #FindBys
like,
#FindBys(xpath)
List<WebElement> elements;
For example if have list of element under UL tag. Then your xpath will be //ul/li/a and you can use it in page factory as given below.
#FindBys(xpath="//ul/li/a")
public List<WebElement> elements;
You can call it in your script or method like, I assume the element is on homepage,
Home homePage = new HomePage(driver);
PageFactory.initElements(driver, homePage);
homePage.elements.get(0).click //to click on first element
or to iterate all elements and get the values as given below.
for(WebElement element:elements)
sop(element.getText();
HTML code:
<div id="routingPanel" class="">
<div id="routingPanelRight">
<ul id="routingList" class="ui-sortable">
<li class="ui-menu-item ui-draggable" style="display: list-item;" role="presentation" data-type="srl" data-id="15">
<a class="ui-corner-all" tabindex="-1">AS-HTTS-US-LAN-SW</a>
<span class="fa fa-trash"/>
<span class="type">[srl]</span>
</li>
<li class="ui-menu-item ui-draggable" style="display: list-item;" role="presentation" data-type="queue" data-id="119">
<a class="ui-corner-all" tabindex="-1">AS-EMEA-NORTH</a>
<span class="fa fa-trash"/>
<span class="type">[queue]</span>
</li></ul></div></div>
I need to click on a button which is having the span class"fa fa-trash" but it is inside li class. And i have list on buttons on the page with li class changing.
I am giving testdata from excel file so i can't use the direct value.
i tried to use this xpath
.//*[#id='routingList']/li[5]/span[1] //testdata1
.//*[#id='routingList']/li[2]/span[1] //testdata2
where li value changes everytime from excel file.
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.visibilityOfElementLocated((By.xpath("//ul[#id='routingList']/li/span[1]")))).click();
List<WebElement> options = driver.findElements(By.xpath("//ul[#id='routingList']/li/span[1]"));
for (WebElement option : options) {
if(testData.equals(option.getText()))
option.click();
Tried above code but it is deleting only one from the list ,where i have passed two more testdata that needs to be deleted.
Need suggestions Please
According to the information you gave me in comments, I think the problem is that you are trying to get a text from an element that doesn't contain text.
Let's say your testData is AS-HTTS-US-LAN-SW. In the HTML you provided and the xpath you mentioned, you are selecting an autoclosing tag <span class="fa fa-trash"/>. Once this tag is selected, you are trying to get the text inside of it, and there is none.
<ul id="routingList" class="ui-sortable">
<li class="ui-menu-item ui-draggable" style="display: list-item;" role="presentation" data-type="srl" data-id="15">
===========================
<a class="ui-corner-all" tabindex="-1">AS-HTTS-US-LAN-SW</a> ----> The text is contained here
<span class="fa fa-trash"/> ---> No text in that tag
===========================
<span class="type">[srl]</span>
</li>
</ul>
So, basically, you have to modify a little bit your xpath from : //ul[#id='routingList']/li/span[1] to : //ul[#id='routingList']/li/a to get the text, and then go back to the parent node to find your button with : ../span[contains(#class, 'fa fa-trash')]
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.visibilityOfElementLocated((By.xpath("//ul[#id='routingList']/li/span[1]")))) // removed the click here because you were clicking on the first element of the list
List<WebElement> options = driver.findElements(By.xpath("//ul[#id='routingList']/li/a"));
for (WebElement option : options) {
if(testData.equals(option.getText()))
option.findElement(By.xpath("../span[contains(#class, 'fa fa-trash')]")).click();
Tell me if it helped
I know you already accepted an answer but there's a more efficient way to do this. You can specify the text you are looking for as part of the XPath. So, you do a single search instead of looping through all the options which can be a performance hit if there are many options. Also, with something like this you are likely to use it more than once so put it in a function.
In this case, the function would take in the string you are looking for and then click the appropriate element.
public void selectRegion(String regionName)
{
driver.findElement(By.xpath("//a[.='" + regionName + "']/following-sibling::span[#class='fa fa-trash']")).click();
}
and you would call it like
selectRegion(testData);
The function looks for an A tag that contains the desired text and then clicks the sibling SPAN with class fa fa-trash.
I am well aware that we can use Actions class to move to a particular web element as below.
Actions(driver).moveToElement(driver.findElement(By.xpath("element"))).build().perform();
For one of the webelement as soon as I move to that element I should get a dropdown. But some times this is working and some times not. Is there any option other than Actions class to simulate mouse movement to particular webelement?
The HTML code for the element I am trying to simulate mouse pointer movement to it is one with class="dropdown-submenu"
<li ng-show="setStatusPermission" class="dropdown-submenu">Set status
<ul class="dropdown-menu" role="menu">
<li ng-show="activePermission" class="">Active</li>
<li ng-show="cancelPermission" class="">Cancelled</li>
<li ng-show="completePermission" class="ng-hide">Completed</li>
<li ng-show="errorPermission" class="ng-hide">Error</li>
<li ng-show="expiredPermission" class="ng-hide">Expired</li>
<li ng-show="suspendPermission" class="ng-hide">Suspended</li>
</ul>
</li>
Automation code snipet to perform the operations is as below.
WebElement webElementSetStatus = basePage.waitForElementToLoadAndReturnWebElement_BasePage(
bc_ui_TaskAction_Setstatus, "linkText", "15");
basePage.getActions_BasePage().moveToElement(webElementSetStatus).build().perform();
List<WebElement> options = basePage.getWebElements_BasePage(
"#bc-td-actions-dropdown-list > li.dropdown-submenu > ul > li", "cssSelector");
for (WebElement option : options)
if (!option.getAttribute("class").equals("ng-hide") && !option.getText().isEmpty())
actualAvailableStatusOptions.add(option.getText());