<div class="info">
<span class="label">Establishment year</span>
"2008"
</div>
我想使用xpath提取2008,但是表达式只选择了建立文本。
driver.find_element_by_xpath("//*[text()='Establishment year']")
答案 0 :(得分:1)
由于文本 2008 位于文本节点内,因此可以提取文本 2008 ,您可以使用以下解决方案:
print(driver.execute_script('return arguments[0].lastChild.textContent;', WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='info']/span[@class='label' and text()='Establishment year']/..")))).strip())
答案 1 :(得分:1)
不幸的是,WebDriver不允许find_element函数结果成为文本节点,因此您必须使用execute_script函数,例如:
driver.execute_script(
"return document.evaluate(\"//div[@class='info']/node()[3]\", document, null, XPathResult.STRING_TYPE, null).stringValue;")
演示:
更多信息: