Get xpath under particular tag - selenium

Am newbie to selenium (java).
I have to click on the menu item which is under <ul> tag as <li> items. I can able to get it with //*[#id='MainMenu']/li[5]/span xpath.
I do not want to hard code [5] of the list item, because item's position may change. It may not at 5th position all the time.
I wanted to get xpath for the particular item under particular tag with an id.
Edit:
This is how my html looks like. List item text will be loading dynamically.
<ul id="sfMainMenu" class="sf-menu ui-selectable">
<li class="ui-selected ui-selectee">
<span subnav="0" param="cmd=desk" filesrc="/Dashboard/Index"></span>
</li>
<li class="ui-selectee"></li>
<li class="ui-selectee"></li>
<li class="ui-selectee"></li>
<li class="ui-selectee">
<span subnav="18" param="cmd=desk" filesrc="../myFile.aspx"></span>
</li>
</ul>
Kindly suggest the approach with an example.

I suggest trying this:
//*[#id='MainMenu']/li[normalize-space() = 'The text you want']/span
Though if you could show us what the HTML in question actually looks like, we can provide a more reliable answer. XPath doesn't have any concept of "visual text", so if you have some text that's hidden within the li you're trying to retrieve, that could be considerably trickier.

I have solved by using filesrc attibute of span tag in the list item.
Xpath is:
//span[contains(#filesrc, 'myFile.aspx')]
As my .aspx file will be the same for any page, so I used filesrc attribute which contains the actual file name and is the only file name in that html page.

You can use the following
driver.FindElement(By.XPath("//li[contains(Text(),'Expected Text']")).Click();
Or you can avoid xpath all together by running a foreach loop on the relevant Class tag
foreach (IWebElement e in driver.FindElements(By.ClassName("ui-selectee")))
{
if (e.Displayed && e.Text == "Expected Text")
e.Click();
}

Related

Click on parent element based on two conditions in child elements Selenium Driver

Using Selenium 4.8 in .NET 6, I have the following html structure to parse.
<ul class="search-results">
<li>
<a href=//to somewhere>
<span class="book-desc">
<div class="book-title">some title</div>
<span class="book-author">some author</span>
</span>
</a>
</li>
</ul>
I need to find and click on the right li where the book-title matches my variable input (ideally ignore sentence case too) AND the book author also matches my variable input. So far I'm not getting that xpath syntax correct. I've tried different variations of something along these lines:
var matchingBooks = driver.FindElements(By.XPath($"//li[.//span[#class='book-author' and text()='{b.Authors}' and #class='book-title' and text()='{b.Title}']]"));
then I check if matchingBooks has a length before clicking on the first element. But matchingBooks is always coming back as 0.
class="book-author" belongs to span while class="book-title" belongs to div child element.
Also it cane be extra spaces additionally to the text, so it's better to use contains instead of exact equals validation.
So, instead of "//li[.//span[#class='book-author' and text()='{b.Authors}' and #class='book-title' and text()='{b.Title}']]" please try this:
"//li[.//span[#class='book-author' and(contains(text(),'{b.Authors}'))] and .//div[#class='book-title' and(contains(text(),'{b.Title}'))]]"
UPD
The following XPath should work. This is a example specific XPath I tried and it worked "//li[.//span[#class='book-author' and(contains(text(),'anima'))] and .//div[#class='book-title' and(contains(text(),'Coloring'))]]" for blood of the fold search input.
Also, I guess you should click on a element inside the li, not on the li itself. So, it's try to click the following element:
"//li[.//span[#class='book-author' and(contains(text(),'{b.Authors}'))] and .//div[#class='book-title' and(contains(text(),'{b.Title}'))]]//a"

not able to select the dependent dropdown value in selenium

system is giving me unable to locate element error message
scenario is i have to select the occupation list which contains values like salaried, self employed(Business) , Self employed(Professional)etc.. on the basis of which the value values will be populated in next dropdown list i am using below code.
driver.findElement(By.id("select2-ED_EmpType-container")).click();
Thread.sleep(4000);
driver.findElement(By.xpath("//*[text()='Salaried']")).click();
the above code is working for other dropdowns bt not working for this dropdown
properties of this dropdown
<ul id="select2-ED_EmpType-results" class="select2-results__options" role="tree" aria-expanded="true" aria-hidden="false">
<li class="select2-results__option" role="treeitem" aria-selected="false"> Please select... </li>
<li id="select2-ED_EmpType-result-nhlf-Salaried" class="select2-results__option" role="treeitem" aria-selected="true"> Salaried </li>
It seems there is space in your string use below xpath
//li[normalize-space() ='Salaried']
To click on the option item Salaried you can use either of the following line of code :
cssSelector :
driver.findElement(By.cssSelector("li.select2-results__option#select2-ED_EmpType-result-nhlf-Salaried")).click();
xpath :
driver.findElement(By.xpath("//li[class='select2-results__option' and id='select2-ED_EmpType-result-nhlf-Salaried']")).click();

Robot & Selenium Tree Element Expansion

My supervisor has recently switched us from HP-UFT to Robot with the Selenium Webdriver and I'm finding it extremely difficult to figure out how to use the driver and/or keyword framework to expand a folding tree ul/li webelement.
Below is a stripped out example of the code on the page:
<ul>
<li class="node-class open" id="i1454430045821320">
<a class="" style="" href="">
<ins> </ins>Location Header 1</a>
<ul>
<li class="node-instance leaf" id="i1454430058247421">
<a class="" style="" href="">
<ins> </ins>Location 1</a>
</li>
<li class="node-instance last leaf" id="i14545337159411690">
<a class="" style="" href="">
<ins> </ins>Location 2</a>
</li>
</ul>
</li>
<li class="node-class closed " id="i14544407827351156">
<a class="" style="" href="">
<ins> </ins>Location Header 2</a>
</li>
What I'm trying to do should be extremely simple: I want to expand a specifed closed tree structure if it's classed as closed. Beneath each branch is an optional nested branch, or a selection checkbox. My end goal is to be able to drill down to a location in the tree that the tester specifies and click the end leaf.
Using "Click Element|xpath=/ul/li[a[contains(text(),'Location Header 2')]" does expand the branch but it also selects all of the child node checkboxes.
In UFT, if I hit this kind of problem I'd simply change the parent <li> class to force it open (if I couldn't click or use any of the other methods to drill down and select).
In Robot, I can use the keyword "Get Element Attribute" to READ the class, but I don't see a keyword to CHANGE it so that idea is out. What I'm looking for is a way to expand the tree branches without inadvertently selecting all of the child nodes.
The drilling down through the tree portion I can deal with once I figure out how to open the nodes correctly but opening up the branch without potentially selecting all of the sub-items is making me pound my head into my desk.
I keep thinking that maybe I'm missing something simple. Any assistance on something that I could try would be greatly appreciated.
Figured it out! Thank you all so much for the ideas.
Click Element At Coordinates | xpath=//ul/li[a[contains(text(),'Location Header 2')] | -100 | 0
Luckily all of the ul element columns on the page have the same css style which sets them at 200px width. I figured through trial and error that the "At Coordinates" option clicks from the center of the object (and not the top left as I originally thought). I figured I'd share this in case anyone else ever has a similar problem.

How to verify the number of search results with Selenium IDE?

I am using Selenium IDE Firefox extension and I need to know how can I verify that the number of elements from a certain area is the correct one. For example, I need to verify that the number of displayed search resuls is equal to 20.
*info updated: there is no table, the results ("li"s) are between "ul" tags. So I think that a command for counting the number of "li"s from that "ul" will work fine.
Here is how the code looks like:
<ul class="testclass">
<li class="search-result"></li>
<li class="search-result"></li>
<li class="search-result"></li>
</ul>
Thanks !

Selenium usage Findelement for list items

I need to click on a element on list item to go to next screen .
Html code for that particular element looks like
<ul>
<li id="leftSiderBarForm:tokenNoListId">
<li id="leftSiderBarForm:patientCheckinScreenId">
<li id="leftSiderBarForm:checkedInPagingId">
<li id="leftSiderBarForm:priorRegLink">
<a onclick="if(typeof jsfcljs == 'function') {jsfcljs(document.forms['leftSiderBarForm'],'leftSiderBarForm:j_id116,leftSiderBarForm:j_id116,regisType,4,registrationCategory,12','');}return false" href="#">Registration</a>
</li>
I tried using f1.findElement(By.partialLinkText("Registration")).click() not able to open locate.
Please help
Try This....
f1.findElement(By.xpath("\\a[text()='Registration']")).click();
Try like This...
f1.findElement(By.LinkText("Registration")).click();
Have you tried waiting for the element,
new WebDriverWait(f1,30).until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Registration"))).click();
It is possible, that there is another link with text Registration available, but it is either hidden or not doing the thing you expect. Anyway, try to make sure that you are getting the correct element.
You might try (from example by #user3283976, but slashed the right way):
f1.findElement(By.xpath("//a[text()='Registration']")).click();
Or
//li[#Id='leftSiderBarForm:priorRegLink']/a
Or
//a[#contains(#onclick, "registrationCategory")] // make sure that contains is unique
Also, when using double quotes and you have double quotes in your string, don't forget to escape them. Ie:
f1.findElement(By.xpath("//a[#contains(#onclick, \"registrationCategory\")]")