Selenium usage Findelement for list items - selenium

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\")]")

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"

Selenium XPATH selecting next sibling

<div class="block wbc">
<span></span>
<span> text_value </span>
</div>
for getting text in second span where does below code go wrong?
driver.find_element(X_PATH,"*//div[#class='block']/span[1]")
For trying by yourself, maybe I write sth wrong here is link
https://soundcloud.com/daydoseofhouse/snt-whats-wrong/s-jmbaiBDyQ0d?si=233b2f843a2c4a7c8afd6b9161369717&utm_source=clipboard&utm_medium=text&utm_campaign=social_sharing
And my code; still giving an error
playbackTimeline__duration =driver.find_element(By.XPATH,"*//div[#class='playbackTimeline__duration']/span[2]")
For finding web element clearly:
//*[#id="app"]/div[4]/section/div/div[3]/div[3]/div/div[3]/span[2]
But I will not use this way, I need declare with class method or CSS Selector at least
If you are sure that you always need the second span use this XPath:
*//div[#class='playbackTimeline__duration']/span[2]
If you need the first span that has actual text use this:
*//div[#class='playbackTimeline__duration']/span[normalize-space()][1]
If the #class has more than only playbackTimeline__duration in it you can use:
*//div[contains(#class,'playbackTimeline__duration')]/span[2]
If there are more div's like that use:
*//div[contains(#class,'playbackTimeline__duration')][1]/span[2]

Need Xpath expression for selecting Create Task option please help me with this

Need Xpath expression for selecting Create Task option please help me with this:
<ul class="container" style="padding-left:5%;padding-top:0.5%;padding-bottom:0.5%;">
<li id="createTask">
<a onclick="createTask();">
Create Task
</a>
</li>
If you really want to solve this using xpath, you may rely on the li tag and it's id:
//li[#id="createTask"]/a
Note that more explicit and reliable way would be to find the link by Partial Link Text. Example in java:
WebElement createLink = driver.findElement(By.partialLinkText("Create Task"));
createLink.click();

Get xpath under particular tag

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();
}

Unable to click the elements in the dynamical list

For the below code
<li class="user ui-menu-item" role="presentation">
<a id="ui-id-52" class="ui-corner-all" tabindex="-1">
<em>User:</em>
Staff User
</a>
</li>
This is the scenario:
There is a text field, in which I enter the name Staff, with that the related values for the staff are displayed in the dynamic list box, in the above scenario the id is dynamically generated, and when I tried to select the value by class, it is same for all the elements.
I want an xpath expression to select the first available options in the list. I tried in many ways like with contains and starts-with, but no use. Please let me know your valuable suggestion.
Thanks in Advance
Shiva.
I think this should work
WebElement ele = webdriver.findElement(By.Xpath("//li[#class='user ui-menu-item'][1]"))
Did you try forming a List and then just directly using the first element of that List?
List<WebElement> list = driver.findElements(By.xpath("//li[#class(contains, 'user')]";
list[0].getText();
I'm still not 100% percentage sure what you want to do with the element once you have found it though. It would probably be better to form the List, then iterate over that list and perform whatever action you require per element.
Use this xpath
"(.//a[#class='ui-corner-all'])[1]"
source: XPath query to get nth instance of an element
Use the xpath. It should work
//li[#role = 'presentation']//a[1]