I'm trying to verify the tool tip text using selenium for the following HTML code.But not sure how to proceed further.
HTML CODE:
<div id="divImgAnnualAllowanceType" class="imgHelp" _tooltip="If the client
<br>an arrangement
<br>a durable arrangement
<br>received a Lump Sum.">
</div>
Since the text is separated by <br> I don't know how to retrieve this text.
I tried using the following code but got null value.
driver.findElement(By.id("divImgAnnualAllowanceType")).getAttribute("value");
Thanks in Advance :)
<div> tags doesn't (necessarily) has value attribute, use getText() instead
driver.findElement(By.id("divImgAnnualAllowanceType")).getText();
You should specify the attribute which value you need.
driver.findElement(By.id("divImgAnnualAllowanceType")).getAttribute("_tooltip");
You have to use proper attribute name with getAttribute() method and there is no attribute as value in your targeted <div> element, you can try below code:
String tooltip = driver.findElement(By.id("divImgAnnualAllowanceType")).getAttribute("_tooltip");
System.out.println(tooltip.replace("<br>", ""));
In above code, we are storing the tooltip value in a variable then printing the same after replacing all <br>.
This code is written in Java.
Related
I am trying to get value from a text field using selenium, but I am not able to. The value is neither present between the tags nor in the attribute "value".Please help me with this.
I have tried following ways but nothing worked.
Webelement.getAttribute("innerText");
Webelement.gettext();
Webelement.getAttribute("value");
Webelement.getAttribute("textcontent");
Below is the HTML for the text field.
<input name="quantityField_valueFieldKeyboard" id="quantityField_valueFieldKeyboard" data-mini="true" data-clear-btn="false" maxlength="61" seyctype="numeric" class="seyc-visually-important seyc-ui-input-icon-white seyc-ui-input-text">
is this what you are trying?
driver.findElement(By.id("quantityField_valueFieldKeyboard")).getAttribute("value");
driver.findElement(By.id("quantityField_valueFieldKeyboard")).getAttribute("value");
is correct way. You are getting empty string is also correct because your text field has no data in it. Since this is an input field, first enter some text and once text is saved. Try to fetch the entered text you will get your desired results.
I have html code:
<div class="formCaptionContainer fill-width" data-dyn-bind="sizing: { width: $dyn.layout.Size.available }">
<h1 class="formCaption" data-dyn-bind="text: $data.Caption, click: $data.GoGridView">Expense report for Aditi Mehta - GS1-000282, testing2</h1>
<h2 class="formCaption-context" data-dyn-bind="text: $data.ParentTitleFields">Aditi Mehta : GS1-000282</h2>
</div>
I want to get the value Expense report for Aditi Mehta - GS1-000282, testing2 from /h1 tag
Any one know how to do it?
I've tried :
By.xpath(".//div[#class='formCaption']/h1";
Above showing no element found
By.className("formCaption");
Above showing blank data
By.xpath(".//*[#class='formCaption']/h1");
Above showing no element found
This code work for me very well
String textprint=driver.findElement(By.xpath("//h2[#class='formCaption-context']")).getText();
System.out.println(textprint);
Hope It will solve your problem
Try this one
String msg= driver.findElement(By.xpath(//div[contains(#class='formCaptionContainer')]/h1 ).getText();
Can you try the below Xpath.
String msg=driver.findElement(By.xpath("//div[#class='formCaptionContainer fill-width']/h1[#class='formCaption-context']")).getText();
As per the HTML you have shared, the node attributes looks dynamic to me. So we have to induce WebDriverWait as follows :
new WebDriverWait(driver, 10).until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//h1[#class='formCaption']"), "Aditi Mehta - GS1-"));
System.out.println(driver.findElement(By.xpath("//h1[#class='formCaption']")).getAttribute("innerHTML"));
You need to consider your locator strategy, the following provide good preferences for general case approach.
location by ID globally unique elements
locate by name for page unique elements
locate by css as catch all case
You should also take a look at the PageObject pattern.
It appears the page is using a JS library to display the contents through late binding/resolution. Your test needs allow for this and wait until the page elements you are interested in are fully rendered. Find out from the developers what the element looks like before it is resolved and afterwards. Late resolution is one considerations in determining your locator strategy. The strategy should include fluent waiting and default time-outs for elements to appear.
This is not a problem of fixing a single locator.
Thanks all for your help. It is working fine now. I use the below code
By headingExpenseText = By.xpath("//*[#class='formCaption'][contains(text(),'Expense report for')]");
public String getTitleEx()
{
return driver.findElement(headingExpenseText).getAttribute("innerHTML");
}
This worked for me:
String titleElem = driver.findElement(By.xpath("//*[#class='formCaptionContainer fill-width']/h1")).getAttribute("innerHTML");
assertEquals("Worked", titleElem);
What do you need exactly, Data displayed on web page or textValue used in HTML code?
If your locator is correct , then instead of getText(), you can try getAttibute("innerHTML") like below,
By.xpath("//*[#class='formCaption'][contains(text(),'Aditi Mehta')]").getAttribute("innerHTML");
How to get translate text value from an Angular tag using selenium Java. Specifically saying, I want the value coming in translate tag
<h5 id="myModalLabel" class="modal-title">
<span translate="EMAIL_MSG"/>
Please enter your email address. </h5>
The translate tag is returning value "An email with instructions to reset your password will be sent to test.test#test.com . Please check your email shortly."
translate is an attribute, not a tag. Use getAttribute() method to get the value
String value = driver.findElement(By.cssSelector("#myModalLabel > span")).getAttribute("translate");
Not sure what are you asking here by getting translate text value. Did you try simply using getText()?
This should simply return you the text you need.
driver.findElement(By.xpath("//*[#id='myModalLabel']/span")).getText();
I have the following html tag and I want to receive "name":"test_1476979972086" from my Java Selenium code.
How can I achive this?
I already tried getText and getAttribute function but without any success.
<a data-ng-href="#/devices"
target="_blank"
class="ng-binding"
href="#/devices">
{"name":"test_1476979972086"}
</a>
getText() is always emtpy. The xpath is unique. newDevice.created is unique on page.
final By successMessageBy = By.xpath("//p[#data-ng-show='newDevice.created']/a");
final WebElement successMessage = wait.until(ExpectedConditions.presenceOfElementLocated(successMessageBy));
final String msg = successMessage.getText();
Actually WebElement#getText() returns only visible text. It could be possible element is present there but text would be visible later.
So if WebElement#getText() doesn't work as expected, you should try using getAttribute("textContent") as below:-
successMessage.getAttribute("textContent");
upon first glance, the below should work. the fact that what you've tried doesnt work, leads me to believe that you aren't selecting the correct element. since i am ignorant of the rest of your html, this might not be unique. you'll have to play around with it, or share the surrounding html
String json = driver.findElement(By.cssSelector("a[href$='/devices']")).getText()
I'm using Java and Selenium. I know I could get visible text using the getText() method/ For example, if I supply the ID (via XPath or CSS) I could do getText() on this to get visible text. But what if I want to do the opposite. Let's say I know what the visible text is and I want to use the text to find another attribute? For example, let's say I have the following HTML markup:
<div class="" title="Card"/>
<div id="99999cardName" class="cardName editInline" title="Click to edit">ZZZ</div>
<div id="99999cardNumber" class="cardNumber">4590 6565 6565 6565</div>
In this example, suppose I knew the value 4590 6565 6565 6565 and I wanted to get the value 99999cardNumber - how would I do it?
Thanks
Use xpath in such a way that it contains text of that element. Here's how to do it -
String idVal = driver.findElement(By.xpath("//div[contains(text(),'4590 6565 6565 6565')]")).getAttribute("id");
System.out.println(idVal);
Hope this helps.