Getting text from a node
You can't do this in the WebDriver API, you have to do it in your code. For example:
var textOfA = theAElement.getText();var textOfSpan = theSpanElement.getText();var text = textOfA.substr(0, textOfA.length - textOfSpan.length).trim('\n');
Note that the trailing newline is actually part of the text of the <a>
element, so if you don't want it, you need to strip it.
Here is the method developed in python.
def get_text_exclude_children(element): return driver.execute_script( """ var parent = arguments[0]; var child = parent.firstChild; var textValue = ""; while(child) { if (child.nodeType === Node.TEXT_NODE) textValue += child.textContent; child = child.nextSibling; } return textValue;""", element).strip()
How to use in this:
liElement = driver.find_element_by_xpath("//a[@href='your_href_goes_here']")liOnlyText = get_text_exclude_children(liElement)print(liOnlyText)
Please use your possible strategy to get the element, this method need an element from which you need the text (without children text).