使用xpath提取<br/>标记后的文本

2024-03-29 00:00:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下一个HTML。我想用xpath提取名字,在我的例子中是Steve。我使用Selenium Python。我该怎么做?在

<section class="PersonalCard">
  <!--<h2></h2>-->
  <div class="clr" style="height:25px"/>
   <span>Speciality:</span>
   <br/>
   Steve 
   <div class="clr" style="height:40px"/>
   <h3>Details</h3>
   <table cellspacing="0" cellpadding="0" border="0">
     <div class="clr" style="height:40px"/>
     <div class="clr">
</section>

Tags: divstylehtmlseleniumsectionh2名字xpath
2条回答

您可以使用下面的方法通过执行javascript来获取文本

element = driver.find_element_by_xpath("//section[@class='PersonalCard']/div[@class='clr']")
print(driver.execute_script("return arguments[0].childNodes[4].textContent", element).strip())

您是否尝试过类似的xpath:

el = driver.find_element_by_xpath('//div[@class="clr"]/span/br')

相关问题 更多 >