<a ui-sref="apps.service" href="#/apps/service">
<i class="glyphicon glyphicon-list icon zoom-icon text-info-dker"></i>
<span class="font-bold"> Service</span>
</a>
How can I locate the element?
driver.findElement(By.partialLinkText("service")).click();
driver.findElement(By.xpath("ui[href$=services]"));
partialLinkText is case sensitive. Try
driver.findElement(By.partialLinkText("Service")).click();
The element is in <a> tag, not <ui> tag
driver.findElement(By.xpath(".//a[href*='services']"));
You should try using xpath with WebDriverWait to wait until element visible and enable as below :-
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath(".//a[contains(.,'Service')]"))).click();
It is better to use contains in to locate element in this xpath. The below xpath will be accurate.
driver.findElement(By.xpath("//a/span[contains(text(),'Service')]")
This XPath,
//a[normalize-space() = 'Service']
will select all a elements whose normalized string value equals "Service" -- perfect for the example you show.
Be wary of solutions based upon contains() as they will also match strings that include additional leading or trailing text, which may or may not be what you want.
Related
How to get exract xpath for the following name(I1888 - Child 1.1). now i am using following xpath "//span[contains(#class,'TreeTitleRed')][contains(.,'Child 1.1')]" but i need to get xpath without contains. please help me
<div>
<span> class ="Vegan tree"
<span class="treeTitlered">I1888 -Child 1.1</span>
</span>
</div>
You can use the following if you want it in xpath
//span[#class='treeTitlered']
Or you can directly find the locator by using the classname
driver.findElement(By.className(“treeTitlered”));
Hope this helps.
You can also use.
//div/child::span[1]/child::span[1]
This will surely work for you, do let me know in case of any further queries.
You can also use
//div/span/child::span[#class="treeTitlered"]
You can stick to parent <span> tag and locate it using its class attribute of Vegan tree.
Once done you can query its innerText property which will return I1888 - Child 1.1 text
Example code (assumes WebDriverWait just in case)
WebElement veganTree = new WebDriverWait(driver, 10)
.until(ExpectedConditions
.presenceOfElementLocated(By
.xpath("//span[#class='Vegan tree']")));
System.out.println(veganTree.getAttribute("innerText"));
Demo:
I'm trying to find an element by the text it contains, then check that that element also has a link to a particular place. I'm using selenium/java.
I'm trying to find elements by text when I can to minimise how many changes I will need to make if the UI is updated (reduce test maintenance costs).
I've tried the following, but the assert fails as the getAttribute ends up being null.
WebElement newsHeadlineTemplate = driver.findElement(By.xpath("//*[contains(text(), 'News Headline')]"));
Assert.assertEquals("Template not clickable", "/news/create/new", newsHeadlineTemplate.getAttribute("href"));
HTML for element I'm trying to find/use:
<div class="columns">
<div class="column is-one-third">
<p>News Headline</p>
</div>
</div>
I'm still fairly new to selenium so any help is very much appreciated.
Your XPath selector is a little bit wrong, you're matching <p> tag and you need to match the <a> tag which is the following-sibling for the <p> tag.
So you need to amend your expression to look like:
//p[text()='News Headline']/following-sibling::a
More information:
XPath Tutorial
XPath Axes
XPath Operators & Functions
i'm tried to select an element from an auto suggestion field but i got always an error saying that the element could not be found even that i'm sure my xpath is correct
here's my code :
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#class=\"ui-menu-item-with-icon ui-menu-item\"][1]")));
driver.findElement(By.xpath("//*[#class=\"ui-menu-item-with-icon ui-menu-item\"][1]")).click();
it should find //*#class=\"ui-menu-item-with-icon ui-menu-item\" which is the first suggestion albert cammus
here's the outerHtml
<li class="ui-menu-item-with-icon ui-menu-item" role="menuitem">
<a class="ui-corner-all" tabindex="-1">
<span class="item-icon"></span>
Albert Camus (SARCELLES)</a>
</li>"
Your XPath is more or less OK apart from using wildcard which may result into longer processing so you can go for li instead of *.
Another option is sticking to the <a> tag containing the text you would like to click using normalize-space() function something like:
//a[normalize-space()="Albert Camus (SARCELLES)"]
Also your popup may reside within an iframe so you might have to switch the webdriver context to the relevant iframe element.
Why don't you try linkText over Xpath ?
linkText is more stable then Xpath, there's no doubt about that.
Code :
wait.until(ExpectedConditions.visibilityOfElementLocated(By.partialLinkText("Albert Camus (SARCELLES)")));
I'm not very sure about spaces in your HTML, that's the reason why I have used partialLinkText
I would like to retrieve a webelement out of a nested html path using either css selectors or xpath. My specific use case is that I would like to select the i element in the following snippet:
<td class="headerActionsTd" data-rolename="Speaker">
<div class="headerActions">
<span class="addNewParticipantSection">
<i class="icon fa fa-user-plus" title="Add New"></i>
</span>
How do I obtain the i webelement for this using either css selector or xpath?
Use this xpath: //td[#data-rolename='Speaker']//div//span//i[#title='Add New'] or
css : div.headerActions i
driver.findElement(By.cssSelector("div.headerActions i"));
for multiple elements :
List<WebElement> users = driver.findElements(By.xpath("//td[#data-rolename='Speaker']//div//span//i[#title='Add New']"));
A simple one would be this :
CSS_SELECTOR
i.icon.fa.fa-user-plus[title='Add New']
Note that, if there is multiple element with this css selector, then you have this facility to differentiate between them:
:first-child
:nth-child(n)
:nth-last-child(n)
More can be found at this link
XPATH : would be :
//td[#data-rolename='Speaker']/descendant::span[#class='addNewParticipantSection']/i
Hope that helps.
Since you said data-role only changes in the comment of the other person, here is the xpath you can use
"//td[#data-role='Speaker']//i"
Or
"//td[#data-role='Speaker']/div/span/i"
Another option is that you can attempt to locate elements inside other elements. For example, you could store the addNewParticipantSection div in a WebElement, then use that WebElement's findElement method to locate the i element within it. Like so:
WebElement section = driver.findElement(By.className("addNewParticipantSection"));
WebElement icon = section.findElement(By.tagName("i"));
icon.click();
Here is the HTML:
<li>
<input type="checkbox" checked="" name="selectedMstrPrivGroupList[9].mstrAuthorities[0].status"/>
Add Dexter
</li>
How could this element be clicked in WebDriver? It is a check box. And I want to use XPath as I have close to 30+ check boxes in the page. So that I can create a generic method and pass only the WebElement. I tried the following but didn't work.
Driver.findElement(By.xpath("//input[contains(.,'Add Dexter')]")).click();
If the checkbox next to "Add Dexter" is what you want to click on the page, you can use:
Driver.findElement(By.xpath("//li[contains(.,'Add Dexter')]//input[#type='checkbox']")).click();
What is with this one:
Driver.findElement(By.xpath("//input[#name='selectedMstrPrivGroupList[9].mstrAuthorities[0].status']")).click();
You can use like this,
driver.findElement(By.xpath("//li[contains(text(),'Add Dexter')]")).click()
You can use xpath to click on the element as below:
driver.findElement(By.xpath("//input[text()='Add Dexter']")).click();
You can also click on that element by using cssSelector instead of xpath as below:
driver.findElement(By.cssSelector("input:contains(^Add Dexter$)")).click();
Note: CssPath/CssSelector is faster than xpath. So it's better to use cssSelector than xpath in most cases.