How to get text from parent element and exclude text from children (C# Selenium)
This is a common problem in selenium
since you cannot directly access text nodes - in other words, your XPath expressions and CSS selectors have to point to an actual element.
Here is the list of possible solutions for your problem:
- get the parent element's text, for each child, get the text and remove it from the parent's text. What you would have left is the desired text -
Google Link
in your case. - if you want to get the
Google Link
just to make an assertion, it could be that you would be okay with checking if the parent's text starts withGoogle Link
. SeeStringAssert.StartsWith()
. get the
outerHTML
of the parent's text and feed to an HTML Parser, likeHtml Agility Pack
. Something along these lines:string outerHTML = driver.FindElement(By.CssSelector(".linksSection > a#google")).GetAttribute("outerHTML");
HtmlDocument html = new HtmlDocument();
html.LoadHtml(outerHTML);
HtmlAgilityPack.HtmlNode a = html.DocumentNode.SelectNodes("//a[@id='google']");
HtmlNode text = strong.SelectSingleNode("following-sibling::text()");
Console.WriteLine(text.InnerText.Trim());
How to get text of an element in Selenium WebDriver, without including child element text?
Here's a general solution:
def get_text_excluding_children(driver, element):
return driver.execute_script("""
return jQuery(arguments[0]).contents().filter(function() {
return this.nodeType == Node.TEXT_NODE;
}).text();
""", element)
The element passed to the function can be something obtained from the find_element...()
methods (i.e. it can be a WebElement
object).
Or if you don't have jQuery or don't want to use it you can replace the body of the function above above with this:
return self.driver.execute_script("""
var parent = arguments[0];
var child = parent.firstChild;
var ret = "";
while(child) {
if (child.nodeType === Node.TEXT_NODE)
ret += child.textContent;
child = child.nextSibling;
}
return ret;
""", element)
I'm actually using this code in a test suite.
How to use Selenium get text from an element not including its sub-elements
I've seen this question pop up a few times in the last maybe year or so and I've wanted to try writing this function... so here you go. It takes the parent element and removes each child's textContent until what remains is the textNode. I've tested this on your HTML and it works.
/**
* Takes a parent element and strips out the textContent of all child elements and returns textNode content only
*
* @param e
* the parent element
* @return the text from the child textNodes
*/
public static String getTextNode(WebElement e)
{
String text = e.getText().trim();
List<WebElement> children = e.findElements(By.xpath("./*"));
for (WebElement child : children)
{
text = text.replaceFirst(child.getText(), "").trim();
}
return text;
}
and you call it
System.out.println(getTextNode(driver.findElement(By.id("one"))));
Python and Selenium - get text excluding child node's text
You can remove the child node text from the all text
all_text = driver.find_element_by_xpath("//whatever").text
child_text = driver.find_element_by_xpath("//subchild").text
parent_text = all_text.replace(child_text, '')
Get the text from parent element only (without text from children elements) using Selenium in Python?
Try this xpath : .//div[@class='sc-fAyiZu hNLuuN'].getAttribute("innerText").split("")[0]
Get Text from an Element with Selenium (Python)
Does this work ?
wait = WebDriverWait(driver, 10)
desired_text = wait.until(EC.visibility_of_element_located((By.XPATH, "//p[contains(@class, 'sc-')]"))).text
print(desired_text)
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Related Topics
Python Ftp Get the Most Recent File by Date
Os.Walk Without Hidden Folders
How to Remove Items from a List While Iterating
Is There a Difference Between "==" and "Is"
Why Can't I Call Read() Twice on an Open File
How to Force Division to Be Floating Point? Division Keeps Rounding Down to 0
How to Get Posted Json in Flask
Use 'Import Module' or 'From Module Import'
Why Does On_Message Stop Commands from Working
How to Unescape HTML Entities in a String in Python 3.1
Python Subprocess.Popen "Oserror: [Errno 12] Cannot Allocate Memory"
Simulating Key Press Event Using Python For Linux
Of the Many Findelement(S)/By Functions in Selenium, When Would You Use One Over the Other
Static Class Variables and Methods in Python
How to Read a Single Character from the User
Difference Between Old Style and New Style Classes in Python