需要帮助识别正确的 XPath

Need help identifying right XPath

我正在尝试从该网站抓取所有 table:https://qmjhldraft.rinknet.com/results.htm?year=2018

当 XPath 是一个简单的 td(例如名称)时,我可以使用像这样的简单 xpath 来抓取 table:

players = driver.find_elements_by_xpath('//tr[@rnid]/td[4]')

我可以使用此代码抓取玩家姓名:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

PATH = 'C:\Program Files (x86)\chromedriver.exe'
driver = webdriver.Chrome(PATH)
driver.get('https://qmjhldraft.rinknet.com/results.htm?year=2018')

try:
    elements = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, "//tr[@rnid]/td[1]"))
    )
finally:
    players = driver.find_elements_by_xpath('//tr[@rnid]/td[4]')
    
for player in players[:5]:
    pl = player.text
    print(pl)

但是当我到达“高度”部分时,我找不到 write XPath。我想这与 td 有一个 class、“ht-itemVisibility1”有关,改变了抓取它的方式,我尝试了几种不同的方式来抓取它,比如:

('//tr/td[@class="ht-itemVisibility1"][1]')
('//tr/td[@class="ht-itemVisibility1"][5]')
('//tr[@rnid]/td[5]')

无济于事。有人可以启发我使用 td class 捕获此 XPath 的方法吗?非常感谢。

试试这个

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://qmjhldraft.rinknet.com/results.htm?year=2018')

try:
    elements = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, "//tr[@rnid]/td[1]"))
    )
finally:
    players = driver.find_elements_by_xpath('//tr[@rnid]/td[4]')
    
for player in players[:5]:
    pl = player.text
    print(pl)

players_height = driver.find_elements_by_xpath('//tr/td[@class="ht-itemVisibility1"][1]')

for player in players_height[:5]:
    pl = player.text
    print(pl)

players_last_team = driver.find_elements_by_xpath('//tr/td[@class="ht-itemVisibility1"][5]')

for player in players_last_team[:5]:
    pl = player.text
    print(pl)

不知道为什么它对你不起作用,但对我来说效果很好。

结果: