How to get Text from H1 Tag which has no arguments in Web Driver?

Multi tool use
How to get Text from H1 Tag which has no arguments in Web Driver?
<html>
<head>...</head>
<body>
<h1>Phonetic Translator</h1>
<br>
<link rel="stylesheet" href="/style.css" type="text/css">
<title>electRa Phonetic Translator</title>
<p>Today's password is:</p>
<h1>
MQQJXJLCQZ
<hr width="80%">
</h1>
<p>The phonetic translation is:</p>
<h3>
...
</h3>
...
</body>
<html>
Hi,
I want to get the text MQQJXJLCQZ
. As there are two H1 tags after Body. I have used XPath to get the text value but unfortunately I am getting the error message Type mismatch: cannot convert from String to WebElement
MQQJXJLCQZ
Type mismatch: cannot convert from String to WebElement
The code I have written is :
String PasswordxPath = "/html/body/h1[2]/text()";
WebElement H1Element = driver.findElement(By.xpath(PasswordxPath));
WebElement getPassword = H1Element.getText();
Please, can someone correct this code or suggest another way to get the text Value ?
Thanks,
UPDATE1
I have used string to get the text value, but now i am unable to put this value in the form using sendKeys. Error log as below:
Element info: {Using=xpath, value=/html/body/h1[2]/text()} at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
3 Answers
3
Replace WebElement
by String
in the last line :
WebElement
String
String PasswordxPath = "/html/body/h1[2]/text()";
WebElement H1Element = driver.findElement(By.xpath(PasswordxPath));
String getPassword = H1Element.getText();
Please, can you update your question with the new code. How do you call
sendKeys
?– Sébastien Temprado
Apr 21 '17 at 15:22
sendKeys
String PasswordxPath = "/html/body/h1[2]/text()"; WebElement H1Element = driver.findElement(By.xpath(PasswordxPath)); String getPassword = H1Element.getText(); // Paste get Text to Text field WebElement pastePassword = driver.findElement(By.id("SecurityCode")); pastePassword.sendKeys(getPassword);
– Nikhil Oberoi
Apr 21 '17 at 15:37
As said, here is the problem
WebElement getPassword = H1Element.getText();
getText() returns the String value but not WebElement. So you need to use String here, like
String getPassword = H1Element.getText();
Hi I have used string to get the text value, but now i am unable to put this value in the form using sendKeys. Error log as below: Element info: {Using=xpath, value=/html/body/h1[2]/text()} at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
– Nikhil Oberoi
Apr 21 '17 at 14:35
I had a similar problem where getText()
wasn't working.
Try using getAttribute("innerHTML")
getText()
getAttribute("innerHTML")
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Hi I have used string to get the text value, but now i am unable to put this value in the form using sendKeys. Error log as below: Element info: {Using=xpath, value=/html/body/h1[2]/text()} at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
– Nikhil Oberoi
Apr 21 '17 at 14:39