Robin J Martis
Robin J Martis

Reputation: 21

How to get the xpath to get a text without a tag on the same level of other tags

I am trying to get the XPath for Search Results:. However when I giving it //span[@class='zeiss-font-Md aletssubtitle'] it's not detecting the text, it's detecting the span

<div class="col-xs-12">
   <div class="col-xs-5" style="padding:0">
     <div>
       <span class="zeiss-font-Md aletssubtitle" style="line-height:3.2">
          Search Results :
          <label class="ACadmincount"> 1 Matching Results</label>
       </span>
    </div>
   </div>
</div>

Upvotes: 2

Views: 6199

Answers (4)

Eddie Singh
Eddie Singh

Reputation: 58

It's simple, you can try :

//*[text()='Search Results :']

Hope this will help you.

Upvotes: 0

Andersson
Andersson

Reputation: 52685

You cannot use XPath with selenium to get text node like @Sunil Garg suggested (//span[@class='zeiss-font-Md aletssubtitle']/text()). It's syntactically correct XPath expression, but selenium doesn't support this syntax:

selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: The result of the xpath expression "//text()" is: [object Text]. It should be an element.

It you want to get complete or partial text content of a particular element, you can use something like

Python

span = driver.find_element_by_xpath("//span[@class='zeiss-font-Md aletssubtitle']")
span.text # returns 'Search Results : 1 Matching Results'
driver.execute_script('return arguments[0].childNodes[0].nodeValue.trim()', span) # returns 'Search Results :'

Java

WebDriverWait wait = new WebDriverWait(webDriver, 10);
WebElement span = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//span[@class='zeiss-font-Md aletssubtitle']")));
String text = span.getText();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("return arguments[0].childNodes[0].nodeValue.trim()", span);

Upvotes: 1

Priya P
Priya P

Reputation: 123

Can you try using CSS Selector as below:

driver.findElement(By.cssSelector("span.zeiss-font-Md.aletssubtitle)).getAttribute("innerText");

OR

    driver.findElement(By.cssSelector("span.zeiss-font-Md.aletssubtitle)).getText();

Should also work.

Upvotes: 0

Code Guru
Code Guru

Reputation: 15598

To get text value use text() function

Here is the code

//span[@class='zeiss-font-Md aletssubtitle']/text()

Upvotes: 0

Related Questions