How to get text found between span – selenium

0 votes

Inspect element from Google Chrome:

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


    <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:


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


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,210 points

3 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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')]")));

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


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
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
0 votes
0 answers
0 votes
1 answer
0 votes
1 answer

How to get next sibling element using XPath and Selenium for Java?

Below code will help you: Try following-sibling axis : WebElement ...READ MORE

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

How to get HTML Source of WebElement in Selenium WebDriver using Python?

You can read innerHTML attribute to get source of ...READ MORE

answered Jun 13, 2018 in Selenium by Samarpit
• 5,130 points
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,030 points
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
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

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.