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,320 points
76,231 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());

For further understanding, you can refer to the Selenium Certification.

answered Jun 18, 2018 by Samarpit
• 5,910 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,770 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,480 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,870 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,910 points
7,275 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 6, 2020 in Selenium by Sri
• 3,190 points
25,684 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 12, 2019 in Selenium by Vaishnavi
• 1,180 points
1,968 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 25, 2019 in Selenium by Abha
• 28,140 points
39,880 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,140 points
10,121 views
0 votes
1 answer

How to get Tooltip Text in Selenium Webdriver?

To get tooltip text in Selenium WebDriver, ...READ MORE

answered Nov 30, 2020 in Selenium by Gitika
• 65,770 points
2,058 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,500 points
13,673 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,190 points
9,142 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,910 points
7,221 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 3, 2020 in Selenium by Sri
• 3,190 points
7,874 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP