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
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 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 am trying to do some tests using Selenium and I am facing some problems. Suppose I have the following:
<div class="itemize-row">
<p class="subText">
<span class="item-label">Card Color:</span> Mandarin
<span class="item-label">Colored Mug:</span> Red
</p>
</div>
Could anybody tell me how to retrieve data "Red" using XPath or CSS?
This XPath,
//span[#class='item-label' and .='Colored Mug:']/following-sibling::text()[1]
will return "Red" as requested.
Generally speaking, yes, but in Selenium, you cannot point to the text
nodes. XPath expressions have to point to "elements". – alecxe
Ok, to account for Selenium limitations, this XPath,
substring-after(//span[#class='item-label' and .='Colored Mug:']/.., 'Colored Mug:')
which takes advantage of the fact that your target is at the end of the string value of the parent of the label, will also return "Red" as requested.
<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.
The current code has long HTML Xpath values that need to be converted & shortened to a css value:
driver.findElement(By.xpath("html/body/div[1]/div/div[2]/div/form/div[3]/div[2]/button")).click();
driver.findElement(By.xpath("html/body/div[1]/div/div[2]/div/form/div[3]/div[2]/button")).click();
You could possibly have chosen a better xpath expression than this one above. What you have done (without looking at the actual HTML code) is you have written down the complete xpath, is it possible to make it shorter / more robust?
Consider the following example:
<html itemscope itemtype="http://schema.org/QAPage">
<body class = "question-page new-topbar">
<div id="notify-container"></div>
<div id="topbar-wrapper"></div>
<button id="button1"></button>
</body>
</html>
You want to click on button1, you can find it using the complete xpath:
driver.findElement(By.xpath("html/body/div/div/button")).click()
or you can find it using xpath along this element's other attributes, in this case, its id.
driver.findElement(By.xpath("//button[#id='button1']")).click()
or as you wanted, you can use CSS selector:
driver.findElement(By.cssSelector('button[id='button1']')).click()
If you want us to help you with converting your xpath into css selector, you will need to copy and paste your html code in your question as well. Without looking at the actual code, we can not be 100% sure.
You may find the following link useful when trying to convert xpath into css selector.
https://www.simple-talk.com/dotnet/.net-framework/xpath,-css,-dom-and-selenium-the-rosetta-stone/