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

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

Related

Need to find the Css-selector path

I have the path in the HTML style:
<div class="BaseL">
<ul class="list">
<li draggable="true" class="listEntry" id="ItemList.item.0" widgetid="ItemL.item.0" >
The code that I have used is:
driver.findbyElement(By.cssSeelector(".BaseL.list.item.0"))
Its not able to find it by id, tried webdriver.wait (didn't work). But even the cssSelector isnt working, can anyone please help me to find the cssSelector?
Thanks.
If you want to find your element by Id:
driver.findbyElement(By.cssSelector(".ItemList.item.0"));
by Class:
driver.findbyElement(By.className("#listEntry"));
Here is the correct CSS.
You have to mask the . in the li id with \
.BaseL .list #ItemList\.item\.0
You can use either of the following css-selectors:
"div.BaseL>ul.list>li.listEntry[id^='ItemList'][widgetid$='0']"
PS: As it is a draggable element you have to induce WebDriverWait

Best way to locate link with protractor

<a href="someCoolLinkText.com">
<h3 class="main-header">
COOL LINK
</h3>
</a>
What is best way to find and click on this link?
Try to locate required node by its link text:
element(by.linkText('COOL LINK')).click()
Note that you should use text exactly as it appears on page, but not as it appears in HTML source code
Update
You can try to use ExpectedCondition to wait for link appearance in DOM:
var EC = protractor.ExpectedConditions;
browser.wait(EC.elementToBeClickable(element(by.linkText('COOL LINK'))), 5000).click();
You can use this xpath :- //h3[text()='COOL LINK']
The text COOL LINK is within <h3> tag so you can use xpath as :
//a[#href='someCoolLinkText.com']/h3[#class='main-header' and contains(.,'COOL LINK')]

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

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

Unable to select menu item using xpath [Validates in Xpath checker, but webdriver cannot find it]

I am trying to select a menu item...though xpath validates well in Xpath checker, it does not work from WebDriver ...can someone help?
I get Unable to locate element: {"method":"xpath","selector":"//a[contains(text(),'Start Loan Process')]"}
HMTL looks something like this
<div class="bd">
<ul class="first-of-type">
<li id="yui-gen7" class="yuimenuitem first-of-type" groupindex="0" index="0">
<li id="yui-gen8" class="yuimenuitem" groupindex="0" index="1">
<li id="yui-gen9" class="yuimenuitem" groupindex="0" index="2">
<li id="yui-gen10" class="yuimenuitem" groupindex="0" index="3">
<li id="yui-gen11" class="yuimenuitem" groupindex="0" index="4">
<a class="yuimenuitemlabel" href="#">Start Loan Process</a>
I have had very little luck with using the Text() part of the xpath. I would rather use the .Text (C#) or .getText() (java) method.
This is how i would write this in c#:
driver.FindElement(By.Xpath(".//*/a").Text.Contains("Start Loan Process");
The xpath part is of course debateable. It could also be: ".//div/li/a". You can use what you want.
On the other hand you can also try to use findElementsBy(By.classname("yuimenuitemlabel"))
Get them to list of web elements and then filter it using webelement.getText() method.