【问题标题】:Python Splinter Get Value From Table (Following-Sibling)Python Splinter 从表中获取值(跟随兄弟)
【发布时间】:2023-04-09 20:42:03
【问题描述】:

鉴于此代码(“睡眠”实例用于帮助显示正在发生的事情):

from splinter import Browser
import time

with Browser() as browser:
    # Visit URL
    url = "https://mdoe.state.mi.us/moecs/PublicCredentialSearch.aspx"
    browser.visit(url)
    browser.fill('ctl00$ContentPlaceHolder1$txtCredentialNumber', 'IF0000000262422')

# Find and click the 'search' button
button = browser.find_by_name('ctl00$ContentPlaceHolder1$btnSearch')
# Interact with elements
button.first.click()
time.sleep(5)

#Only click the link next to "Professional Teaching Certificate Renewal"
certificate_link = browser.find_by_xpath("//td[. = 'Professional Teaching Certificate Renewal']/following-sibling::td/a") 
certificate_link.first.click()
time.sleep(10)

我现在正试图从该代码运行后显示的表中获取值。我不精通xpath命令,但是根据this question的回复,我试过这些,没有用:

name = browser.find_by_xpath("//td[. ='Name']/following-sibling::td/a")
name = browser.find_by_xpath("//td[. ='Name']/following-sibling::td/[1]")
name = browser.find_by_xpath("//td[. ='Name']/following-sibling::td/[2]")

我尝试了 [2],因为我确实注意到“名称”和包含该名称的单元格之间有一个冒号 (:) 同级字符。我只想要名称本身的字符串值(以及表中的所有其他值)。

在这种情况下,我确实注意到了一个不同的结构(span 在 td 中使用,而不仅仅是 td)(我也尝试了 td span[.='Name']... 但没有骰子):

更新以显示更多细节

<tr>
 <td>
  <span class="MOECSBold">Name</span>
 </td>
 <td>:</td>
 <td>
     <span id="ContentPlaceHolder1_lblName" class="MOECSNormal">MICHAEL WILLIAM LANCE  </span>
 </td>
</tr>

【问题讨论】:

  • 你试过//td[. ='Name']/following-sibling::td/span//td[. ='Name']/following-sibling::td[2]吗?
  • 我刚做了,但没有运气。

标签: python xpath splinter


【解决方案1】:

这最终奏效了:

browser.find_by_xpath("//td[span='Name']/following-sibling::td")[1].value

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多