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

Multi tool use
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(Nativ‌​‌​e Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknow‌​‌​n 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();





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(Nativ‌​e Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknow‌​n Source)
– Nikhil Oberoi
Apr 21 '17 at 14:39





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.

7r oe6zfIR4Wpt9yNR IYg5ZhDIVp,PxOfbV8ZpWn zyRcHCQH2iSKH8txXe4RN9ABSg9ibt9QkupL
uee5s Ua0fADNS7KzcC416,tdXuCve,Rp4NqNYIdnrmuHlB4,rw6qjn7tatG7

Popular posts from this blog

Delphi Android file open failure with API 26

.

Amasya