How to Assert a Text In Regional Language like Hindi in Selenium - java

I want to validate a text but its in Hindi in the website i am working on .
The code goes this way
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.textToBePresentInElementLocated(
By.xpath("/html/body/nav[1]/div/ul/div/div/li[2]/form/input[4]"),
"हिंदी"));
System.out.println("Language changed to "+ landingpage.getHindiLanguage().getText());
And The Output I Get is
Expected condition failed: waiting for text ('?????') to be present in element found by By.xpath:
How shall i approach this?

From your xpath assuming you are trying to getText from a input tag
Input tag stores the value in value attribute so you should get the value from the attribute instead of using getText() use
webelement.getAttribute("value") this will return values stored in input field
getText() will works only for HTML element which store values in node.
Like p tag as shown below
<p>PARAGRAPH</p>

Related

How can I get the last text in <p> through xpath in selenium webdriver

I want to extract the text in the paragraph encircled in the screenshot. But from the last instance of the class, because this text is dynamic. Therefore I want the last text in this class.
I am tried this
String Reply2= driver.findElement(By.xpath("//div[#class='chat-message-content clearfix']/last[]")).getText(); ][1]
you can't use last[]
use [last()]
//div[contains(#class,'chat-message-content')][last()]/p
You are almost there, just need couple of changes in your xpath.
String xPath = "(//div[#class='chat-message-content clearfix']/p)[last()]"
//div[#class='chat-message-content clearfix']/p should get all the p items under chat-message-content clearfix classes. Then group them by using ( and ). Now get the last item from the group using [last()].
Get the text by using the below line.
String Reply2= driver.findElement(By.xpath(xPath)).getText();

Use Selenium Java to get string text from HTML

I want to get Selenium with Chromedriver to recognize and import a line of html text into a Webelement variable.
Given this HTML:
<li id="password_rules">
<strong>Password Rules</strong>
<p>The database password rules conform with...:</p>
<ul>
<li>The password can not be the same as the user id.</li>
<li>Password length must be between 8 and 25 characters.</li>
</ul>
</li>
I want to grab the text in the last list element ("Password length must be between 8 and 25 characters.").
This is the java code I'm attempting to use:
WebElement passwordCriteria = driver.findElement(By.xpath("//*[#id = 'password_rules']//*[contains(text(), 'Password length must be between ')]"));
String lineText = passwordCriteria.getText();
When that Java executes, it returns an error saying the element cannot be found:
Exception in thread "main"
org.openqa.selenium.InvalidSelectorException: invalid selector: Unable
to locate an element with the xpath expression //[contains((text(),
'Password length must be between ')] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string
'//[contains((text(), 'Password length must be between ')]' is not a
valid XPath expression.
Any insight is much appreciated.
If you are grabbing a WebElement by it's id, then you don't need your extra specific xPath. id is supposed to be unique by convention. If you have other html elements with the same id, consider changing them to a class.
You should be able to grab the element like this:
WebElement passwordCriteria = driver.findElement(By.id("password_rules"));
If you're committed to finding the element by the id containing some text then the way you should do it is as follows:
WebElement passwordCriteria = driver.findElement(By.xpath("//*[contains((text(),'Password length must be between')]"));
Also, sometimes Selenium will complain if elements are not visible on the page when you try and reference them. Sometimes you need to wait, other times you need to perform some other action to make the text visible before referencing it, like hovering the mouse over a dropdown, etc.
For example
Suppose the html you pasted here is an error message. This error message does not start out as being 'visible', but instead it is shown after the user types their password in incorrectly. In such an instance, Selenium won't let you reference the text from an element inside this div, since it's not currently view-able. Instead, what you would have to do is use Selenium to input the incorrect password in the fields, wait for the error message to be displayed, and then finally reference the WebElement, only after it is able to be seen.
EDIT:
I misread OP's intention. The element that OP is trying to reference is NOT the element with the id, but rather a child of that element. Instead of rewriting my answer, I will point out that #Grasshopper answer has both css and xPath solutions.
You can try these locators if the concerned li is always the last child.
Css - "li[id='password_rules'] > ul > li:last-child"
xpath - "//li[#id='password_rules']/ul/li[last()]"
As per your question as the desired text is within Password Rules you have to induce WebDriverWait with ExpectedConditions as textToBePresentInElementLocated and then retrieve the text as follows :
new WebDriverWait(driver, 20).until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//li[#id='password_rules']//ul//li"), "Password length"));
String lineText = driver.findElement(By.xpath("//li[#id='password_rules']//ul//li[contains(.,'Password length')]")).getAttribute("innerHTML");
Thank you for the help everyone. I finally got it using the following:
new WebDriverWait(driver, 20).until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//li[#id = 'password_rules']"), "Password length must be between "));
WebElement passwordCriteria = driver.findElement(By.xpath("//li[#id = 'password_rules']/ul/li[2]);
String lineText = passwordCriteria.getText();
Your original example had // which should only be used at the beginning of an xpath.

Select the String value of an element using xpath , in Selenium Java

I am completely new to selenium. I am using Selenium and java.The following is my resulting screen value. I want to select the double quoted data(Test) from this screen, and i want to replace it to a different value.
"value": "Test",**
"createDateTime": "2016-08-23T15:37:06+0100",
"channel": "POST",
Note: the whole data is under a div class =ace_gutter, the parent div class is "aceEditor".
After a long struggle i found the following xpath for that row which i want to get the value from.
HTML code:
#11
Xpath:
It is this
[#id='aceEditor']/div[1]/div[1]/div[11].
myCode:
String value= driver1.findElement(By.xpath("//*[#id='aceEditor']/div[1]/div[1]/div[11]")).getAttribute("innerHTML") ;
It is giving 11, instead of the actual text("value": "Test",)from the screen. Could any one Please help me with the code. I have searched enough i am not finding any solution.
To replace the value in the HTML page, you will have to use the executeScript method.
String replace_text = "Text to be replaced";
js = ((JavascriptExecutor) driver)
js.executeScript("return document.getElementById('<id of the element>').innerHTML='" + replace_text + "';");

How to use getText and get an attribute in Selenium WebDriver

Below are separate textboxt1 IDs for getting the text:
WebElement Text = driver.findElement(By.id("incident.number"));
String result = Text.getText();
How can I write the code for getting the values from gettext?
Below are textbox2 id:
driver.findElement(By.id("38240a5edb5912000c91d540cf9619b9_text"));
I want to get the value from textbox1 to textbox2. How can I get the attributes from getText?
You can get the the value of text box using getAttribute("value") like driver.findElement(By.id("38240a5edb5912000c91d540cf9619b9_text")).getAttribute("value"). This will return you the value of a textbox.
You can get the string in the textbox by getAttribute("value"):
String textboxValue = driver.findElement(By.id("38240a5edb5912000c91d540cf9619b9_text")).getAttribute("value");
If you are able to get the value from the textbox, then just pass that string to textbox2 using sendKeys as below.
driver.findElement(By.id("38240a5edb5912000c91d540cf9619b9_text")).sendKeys(result);
I am supposing that the locator is fine and able to locate the elements.

Selenium getText

I want to getText() using By.id or By.cssSelector.
I managed to solve my problem by doing getAttribute("value"), but I don't understand why getText() doesn't work like I expect it, and I might need it so all help is appreciated.
Here is the Java code:
WebDriverWait wait = new WebDriverWait(driver, 10);
Boolean elementIsPresent = wait.until(ExpectedConditions.textToBePresentInElementValue(By.cssSelector("#general_service_name"),"[reg] general_service_name")); // true
//WebElement general_service_name = driver.findElement(By.cssSelector("#general_service_name"));
WebElement general_service_name = driver.findElement(By.id("general_service_name"));
// Display check
Boolean isDisplayed;
if(general_service_name.isDisplayed()) isDisplayed = new Boolean(true); else isDisplayed = false; //true
String text_empty = general_service_name.getText(); //""
String text_with_value = driver.findElement(By.id("general_service_name")).getAttribute("value"); //"[reg] general_service_name"
And HTML:
<input id="general_service_name" type="text" value="[reg] title" name="general_service_name" style="float:left;"/>
http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebElement.html#getText()
getText() delivers the innerText of a WebElement.
Your input field does not have any inner Text. The text is found inside your value-attribute, hence accessing it via getAttribute("value") is the correct way to do it.
Java
ele.getAttribute("innerHTML");
This could get the text already in the background and not displayed on the page yet.
Simple answer - it's designed this way. getText() parses the content of the tag (i.e. its innerText), which is obviously empty for inputs.
You may use this if you want to search for a given text on a WebElement. Pass it directly or through a string:
String textoToSearch = "Text inside Webelement";
driver.findElement(By.id("someID).getText().contains("textToSearch");
getText()
getText() returns the visible text of this element.
java.lang.String getText()
Get the visible (i.e. not hidden by CSS) text of this element, including sub-elements.
Returns:
The visible text of this element.
As per the HTML of the element:
<input id="general_service_name" type="text" value="[reg] title" name="general_service_name" style="float:left;"/>
The WebElement doesn't have a visible text but the value attribute have the value set as [reg] title.
So to extract the value of the value attribute i.e. [reg] title you can use either of the following Locator Strategies:
Using cssSelector:
System.out.println(wd.findElement(By.cssSelector("input#general_service_name[name='general_service_name']")).getAttribute("value"));
Using xpath:
System.out.println(wd.findElement(By.xpath("//input[#id='general_service_name' and #name='general_service_name']")).getAttribute("value"));
Ideally, you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:
Using cssSelector and getText():
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input#general_service_name[name='general_service_name']"))).getAttribute("value"));
Using xpath and getAttribute("innerHTML"):
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[#id='general_service_name' and #name='general_service_name']"))).getAttribute("value"));

Categories

Resources