<p>另一个好的选择是在xpath中使用<code>ancestor</code>。我将主定位器绑定到表名,这样会更可靠</p>
<p>使用它,您可以找到其他定位器及其文本。只要用正确的路径将它们放入循环即可。
在子xpath中<code>.//td</code>表示元素名为<code>td.</code>的主定位器的直接子级</p>
<p><strong>我的解决方案:</strong></p>
<pre><code>from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
url = 'https://s5.sir.sportradar.com/sports4africa/en/1/season/80526/headtohead/334075/340986/match/27195664'
driver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver')
driver.get(url)
driver.implicitly_wait(10)
WebDriverWait(driver, 15).until(EC.presence_of_all_elements_located((By.XPATH, "//strong[text()='Last matches']/ancestor::div[6]//tbody/tr")))
rows= driver.find_elements_by_xpath("//strong[text()='Last matches']/ancestor::div[6]//tbody/tr")
output = []
for res in rows:
score = res.find_element_by_xpath(".//td[5]//div[@class=' no-wrap']").get_attribute("innerText")
output.append(score)
print(output)
</code></pre>
<p><strong>输出:</strong><br/>
第一<a href="https://s5.sir.sportradar.com/sports4africa/en/1/season/80526/headtohead/334075/340986/match/27195664" rel="nofollow noreferrer">link</a>:
['0:4','3:4','2:2','0:1','3:0','2:2','0:4','1:0','2:1','1:1','1:2','2:4']</p>
<p>第二<a href="https://s5.sir.sportradar.com/sports4africa/en/1/season/82128/headtohead/613958/33714/match/27197856" rel="nofollow noreferrer">link</a>:
['2:0','2:1','0:2','0:0','4:2','1:0','3:0','1:1','1:0','2:1','1:1','0:8']</p>
<p><strong>更新:</strong>
我能做的交换分数的最快方法是分别获得两个分数,将它们放在一个单独的列表中,然后使用<code>zip</code>交换。
结果是两个元组列表</p>
<pre><code>first_score = []
second_score = []
for res in rows:
first = res.find_element_by_xpath(".//td[5]//div[@class=' no-wrap']/div[1]").get_attribute("innerText")
first_score.append(first)
second = res.find_element_by_xpath(".//td[5]//div[@class=' no-wrap']/div[3]").get_attribute("innerText")
second_score.append(second)
first_list = list(zip(first_score, second_score))
second_list = list(zip(second_score, first_score))
print(first_list)
print(second_list)
</code></pre>
<p>结果是两个元组列表</p>
<pre><code>[('0', '4'), ('3', '4'), ('2', '2'), ('0', '1'), ('3', '0'), ('2', '2'), ('0', '4'), ('1', '0'), ('2', '1'), ('1', '1'), ('1', '2'), ('2', '4')]
[('4', '0'), ('4', '3'), ('2', '2'), ('1', '0'), ('0', '3'), ('2', '2'), ('4', '0'), ('0', '1'), ('1', '2'), ('1', '1'), ('2', '1'), ('4', '2')]
</code></pre>
<p>有更有效的方法,但我建议单独问一个问题</p>