How to get text found between span – selenium

0 votes

Inspect element from Google Chrome:

<div class="form-style-abc">

   <fieldset>

    <legend><span class="number">*</span>Search Zone</legend>

I need to retrieve the "Search Zone", however, I’m unable to perform the search and getText(). I had to perform the following on Eclipse but getting an error:

Code:

String HeaderTxt = driver.findElement(By.xpath("//span[@class = 'number']")).getText();

System.out.println(HeaderTxt);


Below is the error message:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//span[@class = 'number']"}
Jun 18, 2018 in Selenium by Martin
• 4,220 points
13,113 views

3 answers to this question.

0 votes

As you are seeing NoSuchElementException.

Possibly you need to wait for the element inducing WebDriverWait as follows :

WebElement HeaderTxtElem = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='form-style-abc']/fieldset/legend[(self::legend) and not (@class='number')]")));

System.out.println(HeaderTxtElem.getText());
answered Jun 18, 2018 by Samarpit
• 5,130 points
0 votes
String HeaderTxt = driver.findElement(By.xpath("//span[@class = 'number']")).text();

VIA PYTHON 3+

answered Jan 28 by TazzProductions
Perfect. This works :) Thanks
+1 vote
The text "Search Zone" is not part of the span tag. Instead it is part of the legend tag.
The span tag holds the text "*".

In Python 2.7:
"""elem == name of element"""
elem = driver.find_element_by_xpath("//legend").text
print elem[1:]
answered Feb 28 by Trying to Figure

Related Questions In Selenium

0 votes
1 answer

How to click a span with given text using Selenium Python

If ALL_USA is subject to change, then ...READ MORE

answered Jul 26, 2018 in Selenium by Samarpit
• 5,130 points
1,928 views
0 votes
1 answer

How to get the text from a website using selenium?

hey, you can use get.text() method to ...READ MORE

answered Dec 31, 2018 in Selenium by Sudhanshu
1,443 views
0 votes
0 answers
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,090 points
777 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
1,022 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
724 views