I want to get the text of an expression in xpath that only has text in its sibling html_code
I'm trying this way but it gives me an error and I don't know how to select the text, since it doesn't have any tag
driver.find_element('xpath','//li[@]//i[@]//following-sibling::text()').text
Error:
InvalidSelectorException: invalid selector: The result of the xpath expression "//li[@]//i[@]//following-sibling::text()" is: [object Text]. It should be an element.
I want to have only " 121 m2 Total" with this.
Any help on this is appreciated.
CodePudding user response:
find_element()
returns a WebElement and it can't return a Text Node. So you can't use:
//xpath_expression/text()
Hence the error:
InvalidSelectorException: invalid selector
Solution
The text 121 m2 Total is within a text node and a decesdent of <i >
. So to extract the text you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:
Using XPATH and text attribute:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//li[@class='icon-feature']/i[@class='icon-stotal']"))).text)
Using XPATH and
get_attribute("textContent")
:print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//li[@class='icon-feature']/i[@class='icon-stotal']"))).get_attribute("textContent"))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python
CodePudding user response:
find_element
must return a webelement, and you cannot convert a text node into a webelement.
In general to get text not wrapped in any tags, you must get the text of the parent and remove from it the text of the children. In this case there is only one child so
child = driver.find_element(By.XPATH, "//i[@class='icon-stotal']")
parent = driver.find_element(By.XPATH, "//i[@class='icon-stotal']/parent::li")
Then
>>> parent.text.replace(child.text,'')
' 121 m total'