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,340 points
47,730 views

6 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,890 points
0 votes
String HeaderTxt = driver.findElement(By.xpath("//span[@class = 'number']")).text();

VIA PYTHON 3+

answered Jan 28, 2019 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, 2019 by Trying to Figure
0 votes

You can use By. XPath with the code you have​ Inner text is text between the opening tags and closing tags. For example: <a>I Am Inner Text</a> In above example, texts “I Am Inner Text” between opening and closing tags are called inner text of web element “a”.

answered Dec 15, 2020 by Gitika
• 65,870 points
0 votes

The text is in the <h2> tag, not the <span> tag. Try

driver.findElement(By.xpath(".//*[@id='iselmf-folder-detail']/h2")).getText();
answered Dec 15, 2020 by Roshni
• 10,420 points
0 votes

Maybe the span element is hidden. If that's the case then use the innerHtml property:

By.css:

String kk = wd.findElement(By.cssSelector("#customSelect_3 span.selectLabel"))
              .getAttribute("innerHTML");

By.xpath:

String kk = wd.findElement(By.xpath(
                   "//*[@id='customSelect_3']/.//span[contains(@class,'selectLabel')]"))
              .getAttribute("innerHTML");

"/.//" means "look under the selected element".

answered Dec 15, 2020 by Rajiv
• 8,880 points

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,890 points
4,826 views
0 votes
2 answers

How to get the text from a website using selenium?

driver.findElement(By.cssSelector("p")).getText() or  IWebElement element = Browser.GetElementByCssSelector("div.loginbox p"); string text = ...READ MORE

answered Sep 5, 2020 in Selenium by Sri
• 3,010 points
14,935 views
0 votes
0 answers

How to get the text from the HTML5 input error message in Selenium?

On giving invalid data in the email ...READ MORE

Mar 11, 2019 in Selenium by Vaishnavi
• 1,180 points
929 views
+1 vote
4 answers

How to get typed text from a textbox by using Selenium Webdriver?

Hey Ashmita, to get the typed text ...READ MORE

answered Jun 24, 2019 in Selenium by Abha
• 28,040 points
17,938 views
0 votes
1 answer

How to switch between tabs on a browser and get titles of each tab using Selenium?

Hi Abhimanyu, you can use following code ...READ MORE

answered Jul 22, 2019 in Selenium by Abha
• 28,040 points
5,426 views
0 votes
1 answer

How to get Tooltip Text in Selenium Webdriver?

Let's see the demonstration of accessing and ...READ MORE

answered Nov 30, 2020 in Selenium by Gitika
• 65,870 points
292 views
0 votes
2 answers

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,520 points
4,925 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,010 points
5,713 views
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,890 points
3,497 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 2, 2020 in Selenium by Sri
• 3,010 points
4,206 views