You can get the parent element text without the child element text as following:
total_text = driver.find_element_by_xpath(parent_div_element_xpath).text
child_text = driver.find_element_by_xpath(child_div_element_xpath).text
parent_only_text = total_text.replace(child_text, '')
So in your specific case try the following:
total_text = driver.find_element_by_xpath("//span[@class='cname']").text
child_text = driver.find_element_by_xpath(//*[@class='multiple']).text
parent_only_text = total_text.replace(child_text, '')
Or to be more precise
father = driver.find_element_by_xpath("//span[@class='cname']")
total_text = father.text
child_text = father.find_element_by_xpath(".//*[@class='multiple']").text
parent_only_text = total_text.replace(child_text, '')
In a general case you can define and use the following method:
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
argument passed here is the webelement returned by driver.find_element
In your particular case you can find the element with:
element = driver.find_element_by_xpath("//span[@class='cname']")
and then pass it to get_text_excluding_children
and it will return you the required text