3

To find my element, I used:

String text = driver.findelement(By.xpath("xyz"));

But it throws an error of type casting. I tried to use the code below to cast the element to a string:

Driver.gettext(text);

This gave me an error that the cast would not work.

How do I get convert the web element to a string?

  • Do you want the text of an element? Otherwise why do you need to convert element into string? – Vishal Aggarwal Sep 15 '18 at 12:24
3

Use:

String text = driver.findElement(By.xpath("xyz")).getText();
| improve this answer | |
  • 2
    Note that the method name is "findElement". Java is case-sensitive. – João Farias Sep 12 '18 at 14:09
0

Are you want to store element as string? Or Element text in String? To store innertText value, you can use

String x = driver.findelements(By.xpath("abc")).getText();
| improve this answer | |
0

Your cast isn't successful because there isn't really anything to cast into a string from a webelement. You might invoke java.lang.objects toString() method but you'll simply get the hash representation of your object.

If you want to extract the text of the Element e.g. "This is a Text" in the following snippet

<div>
    <span id="text"> This is a Text</span>
</div>

you might use driver.findElement(By.xpath("//div/span[@id='text']")).getText();. In my experience getText does sometimes behave quite unpredictable regarding it's return types (sometimes empty strings, sometimes null) which is a serious problem if you use it in a method like

 public Boolean textMatches(String exp, WebElement elem){
     return elem.getText().equals(exp);
 }

because you'll get an uncaught null pointer if it returns null. So I prefer something along the lines of

 public Boolean textMatches(String exp, WebElement elem){
     String val = (String)((JavascriptExecutor)driver).executeScript("return (arguments[0].innerText)?arguments[0].innerText: ''", elem);
     return val.equals(exp);  
  }

since you can be sure that your value will always be a String.

| improve this answer | |
0
List<WebElement> allElements=driver.findElements(By.xpath("//span[@class='dx-checkbox-icon']"));



// which web element you want or you can loop ALL THE VALUES AND GET

String Text =  allElements.get(1).getText();

System.out.Println(Text);
| improve this answer | |
0

Automation testers working in software testing companies follow the below approach to convert Selenium web element to string:

First, convert the required web element into list type web element and then fetch list element in a string using for loop.

List<WebElement> links =driver.findElements(By.xpath("xyz"));

String []linkText =new String[links.size()];
int i=0;

//Now store List elements (one or more) text into String array

for(WebElement a: links)
{
   linkText[i]=a.getText();
   i++;
}

Thanks

| improve this answer | |
  • Can you update your answer? Is first sentence a question? Add some code formatting. – Alexey R. Sep 13 '18 at 10:22
  • Thanks Alexey for pointing out. The answer was added in a hurry. Updated it! – Vishal Sep 21 '18 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.