The XPath 1.0 expression //*[text()]
does not select "now revenue of kfc is "
but the XPath 2.0 expression //text()
does select it
But since selenium only supports XPath 1.0, I can't make it work.
The XPath 1.0 expression //*[text()]
does not select "now revenue of kfc is "
but the XPath 2.0 expression //text()
does select it
But since selenium only supports XPath 1.0, I can't make it work.
You should always post HTML/XML as text, formatted as code, rather than images, and include a MCVE in the body of the question itself. Lacking that, here's an rough, unverified answer to your question...
Here's what the two XPaths you suggest mean:
//*[text()]
says to select all elements with text node children.
//text()
says to select all text nodes in the document.
Neither would appear to be what you want.
Identify something unique and invariant about the context of the target. For example, if the targeted text is always the first text node of a p
with class, c1
, use
//p[@class="c1"]/text()[1]
If it's the first text node preceding such a paragraph, use
//p[@class="c1"]/preceding-sibling::text()[1]
Note that if your class
attribute value could contain multiple class names, use the idiom described in XPath to match @class value and element value? rather than strict equality.
Assuming you want to extract the text, just your first XPath expression (which will select the element containing the text) and .text
. Example with a specific XPath :
element = browser.find_element_by_xpath('//p[contains(@class,"ui_qtext_para")][text()[contains(.,"now revenue of")]]').text
print (element)