GetText() clarification - xpath not returning the value - java

Html:
<strong class="text-xl ng-binding" ng-bind="summary.PublishedIssuedRecent">5</strong>
my xpath:
#FindBy(how = How.XPATH, using = "//strong[#ng-bind='summary.PublishedIssuedRecent']")
public WebElement countRecentlyPublishedApplication;
String pubCount = countRecentlyPublishedApplication.getText();
my Expectation
i want to extract the value of 5
but i'm getting empty value in the pubCount String, Please suggest if any mistake in my xpath

From attribute ng-bind I'm guessing you're trying to scrap angularJS application and you're using selenium web driver.
Problem with angular is, it is rendered by javascript after the page loads. So on beginning element you want to scrap might not be there. Solution might be to wait a second until angulars finishes rendering and then try to find element.
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
WebElement pubCount = driver.findElement(By.xpath( "//strong[#ng-bind='summary.PublishedIssuedRecent']")).getText();

The XPath expression in itself is correct, it selects your node as expected. A simple test with xmllint gives:
> xmllint --xpath "//strong[#ng-bind='summary.PublishedIssuedRecent']" ~/test.html
<strong class="text-xl ng-binding" ng-bind="summary.PublishedIssuedRecent">5</strong>
So the error is outside your XPath expression.
By the way, if you just need the text of the node, you can use
> xmllint --xpath "//strong[#ng-bind='summary.PublishedIssuedRecent']/text()" ~/test.html
5

Related

Unable to locate elements on a website , i tried all the locators but I get the error "No such element" . I am a self taught beginner to Automation

Element HTML:
Inbox
What I tried:
driver.findElement(By.cssSelector("a[text='Log out']"));
then
driver.findElement(By.xpath("//a[.='Log out']"));
Element snapshot:
HTML
driver.findElement(By.linkText("Log out"));
Something like that should work, you should provide the page html for a better response.
My response is based on the fact that in your first try you are saying text='Log out'.
findElement in selenium works with locatorStrategies (By.cssSelector/By.linkText...) the one that i used (linkText) search in all the anchor tags that are found in the pages () and analyze the text inside the tag, if the text is matched the driver will find the element.
Let me know if it works, otherwise provide me the web page html snippet.
I've seen the actual screen, you must change Log out with Inbox
driver.findElement(By.linkText("Inbox"));
Given the HTML:
Inbox
You need to take care of a couple of things here as follows:
Within cssSelector doesn't supports the :contains("text") method
Within xpath for exact text matches you need to use text()
Solution
To identify the element you can use either of the following locator strategies:
Using linkText:
WebElement element = driver.findElement(By.linkText("Log out"));
Using cssSelector:
WebElement element = driver.findElement(By.cssSelector("a[href$='INBOX'][title='View the Inbox']"));
Using xpath:
WebElement element = driver.findElement(By.xpath("//a[text()='Inbox' and #title='View the Inbox']"));

xPath Text Contains Selenium Web Driver

I'm trying to select an element based on its text contents. I am using XPath to achieve this.
I am just puzzled as this should work?
WebElement link = obj.driver.findElement(By.xpath("//div[contains(text(), 'Notifications')]"));
I'll even copy the HTML code:
<div class="linkWrap noCount">Notifications <span class="count _5wk0 hidden_elem uiSideNavCountText">(<span class="countValue fsm">0</span><span class="maxCountIndicator"></span>)</span></div>
The div element has the words "Notifications" inside it. So why doesn't it work.
Go to this page on Facebook: https://www.facebook.com/settings
Use this chrome extension to highlight any area via xPath.
You have a space before the word Notifications:
WebElement link = obj.driver.findElement(By.xpath("//div[contains(text(), 'Notifications')]"));
You should also add a wait for element before trying to find the element:
WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(), 'Notifications')]"));
WebElement link = obj.driver.findElement(By.xpath("//div[contains(text(), 'Notifications')]"));
I found the issue with the help of some amazing people in this community.
Ok, so my element was in an iFrame.
In order to access the element, I must first access the iFrame
WebElement iframe = obj.driver.findElement(By.xpath("//iframe[#tabindex='-1']"));
obj.driver.switchTo().frame(iframe);

How to locate a span with a specific text in Selenium? (Using Java)

I'm having trouble locating a span element in Selenium using java.
the HTML looks like:
<div class="settings-padding">
<span>Settings</span>
</div>
And I've tried the following with no luck:
By.xpath("span[.='Settings']")
and
By.xpath("span[text()='Settings']")
and
By.cssSelector("div[class='settings-padding']"))
as well as some other similar attempts. Could you point me to the best method to do this? As it stands I constantly get "Unable to locate element" error in eclipse.
Your all xpath are looks OK, Just some syntactically incorrect. you are missing // in your xpath
The correct xpath are as below :-
By by = By.xpath("//span[.='Settings']")
Or
By by = By.xpath("//span[text()='Settings']")
Or
By by = By.xpath("//div[#class='settings-padding']/span"))
Or you can use cssSelector as :-
By by = By.cssSelector("div.settings-padding > span"))
Using anyone of the above By locator you can locate element as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement el = wait.until(presenceOfElementLocated(by));
Hope it helps...:)
For the element below
<span class="test-button__text">
Test Text
</span>
The following solution works for me
driver.find_element_by_xpath("//span[contains(#class, 'test-button__text') and text()='Test Text']")

Obtain Selenium WebElement from cssSelector over HTML Input

I am quite new on Selenium (started today) and I would like to get the WebElement corresponding to the following html Input:
<input size="25" style="text-align:center;" value="http" onclick="this.select();" type="text"></input>
And then obtain its value. This is what I have tried so far:
WebElement element = driver.findElement(By.cssSelector(".text-align:center"));
String text = element.getText();
Or this:
WebElement element = driver.findElement(By.cssSelector("input[style='text-align:center']"));
But Java returns in both cases an exception:
org.openqa.selenium.InvalidSelectorException: The given selector
.text-align:center is either invalid or does not result in a
WebElement
Thank you,
Héctor
Do you have to search for the element by cssSelector?
You could give this a try:
WebElement element = driver.findElement(By.cssSelector("input[type='text']"));
If cssSelector is not necessary you could try grabbing the element by xpath.
If you use firefox, there is a plugin called FireBug which allows you to right click after inspecting the element and copying the xpath directly then using :
WebElement element = driver.findElement(By.xpath("XPATH HERE"));
EDIT: Part of post disappeared, redded it.
Your first try is slightly off
driver.findElement(By.cssSelector(".text-align:center"));
The (.) in a CSS selector indicates a CSS class name but that's a style on the element and not a class. There is no class on that element to use in that way.
Your second try looks good but maybe it's not unique on the page? Hard to tell with only the one line of HTML. You'd have to provide more of the HTML of the page. Try it again but get the value instead of text.
WebElement element = driver.findElement(By.cssSelector("input[style='text-align:center']"));
System.out.println(element.getAttribute("value"));
Does that work? You likely will have to provide some unique HTML that surrounds the INPUT that we can use to make the CSS selector more specific.

Selenium Webdriver - Find dynamic id from li/a

I am trying to select a value(Bellevue) from a li(it looks like a dropdown but it isn't).The problem is that its id changes everytime the page loads.
Here is a screenshot:
This time the id is: ui-id-23,but the number,23,will be changed next time so this will not work.If I expand the <a id="ui-id-23..." I get the name 'Bellevue' but every character surrounded by < strong > < /strong > mark-up.
I can't find it after it's classname because both values from li have the same class,ui-menu-item.
I tried after xpath:"//a[contains(text(),'Bellevue')]" but I get the error:Unable to locate element...
Do you know any solution for this?I am using Selenium Webdriver in Java and TestNG.
Thanks!
Update
So I managed to find that element by using:
WebElement value = driver.findElements(By.cssSelector("a[id^='ui-id-']")).get(3);
value.click(); .
but in my application i am using page objects and i look after elements using #FindBy(how.HOW.....).Do you know how I can use .get(3) with #FindBy?
You want to use a CSS selector on the ID:
a[id^='ui-id-']
This says "Find all of the a elements that have an ID that start with ui-id-"
If you want to find the second item, then do:
driver.findElements(By.cssSelector("a[id^='ui-id-']"))[1]
The [1] will select the second item on the page.
It looks like jQuery uniquId() method is used to populated the id, so it will always start with ui-id-. You can use jQuery selector to select element whose id starts with ui-id-
WebElement webElement = (WebElement) ((JavascriptExecutor) webDriver).executeScript("return $( 'input[id^="ui-id-"]').get(0);");
I would try to use xpath avoiding using of id. For example, //a[#class=''ui-corner-all ui-state-focus ][2]
First get the tag name in which your id attribute has been defined.
WebElement ele = driver.findElement(By.tagName(tagName));
String strId = ele.getAttribute("id").startsWith("ui-id-");
driver.findElement(By.id(strId)).click();

Categories