猿问

未找到元素-Selenium-Python

我尝试访问曼联主场的结果,但无法访问。。这是网站页面

下面是该页面的一段 HTML 代码:

<div id="g_1_l2dtbMED" title="Click for match detail!" class="event__match event__match--static event__match--last event__match--oneLine"><div class="event__time">04.10. 17:30</div><div class="event__participant event__participant--home"><svg class="card___2ip_DLm icon--redCard icon--redCard-first icon--redCard-last"><title></title><use xlink:href="/res/_fs/build/symbols.f1bc6b2.svg#card"></use></svg>Manchester Utd</div><div class="event__scores fontBold"><span>1</span>&nbsp;-&nbsp;<span>6</span></div><div class="event__participant event__participant--away fontBold">Tottenham</div><div class="event__part">(1&nbsp;-&nbsp;4)</div><span class="wld wld--l" title="Loss">L</span></div>

我搜索解析的结果是“L”,位于 balise 中<span>。


这是我尝试解析它所做的代码:


driver = webdriver.Chrome()

url = "https://www.flashscore.com/team/manchester-united/ppjDR086/results/"

driver.get(url)


Team = 'manchester Utd'

results = WebDriverWait(driver, 20).until(EC.find_elements((By.XPATH,"//div[@class='event__participant--home' and contains(text(),'"+ Team +"')]//ancestor::div/span")))

print(len(results))

但这在 20 秒后(即搜索时间限制)给我抛出了异常“TimeoutException”。


湖上湖
浏览 132回答 2
2回答

拉丁的传说

您正在寻找的定位器是//div[contains(@class,'event__participant--home')][text()='Manchester Utd']//following-sibling::span[1]^ find a DIV that contains the class indicating a home game&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^ that also contains the team name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^ then find the first sibling SPAN that follows该定位器将找到仅包含主场比赛的 L、W、D 等元素。如果您要等待元素,您将需要等待可见,而不是存在。存在是指元素仅位于 DOM 中但不一定可见。如果要从页面上刮掉文本,则需要等待可见。您可以使用EC.visibility_of_all_elements_located(). 请参阅文档。如果您尝试在它们存在但不可见时抓取页面,则会引发异常。您更新的代码如下driver = webdriver.Chrome()url = "https://www.flashscore.com/team/manchester-united/ppjDR086/results/"driver.get(url)Team = 'Manchester Utd'results = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH,"//div[contains(@class,'event__participant--home')][text()='" + Team + "']//following-sibling::span[1]")))print(len(results))

qq_花开花谢_0

试试这个 xpath-//div[contains(text(),'Manchester&nbsp;Utd')]/following-sibling::span
随时随地看视频慕课网APP

相关分类

Python
我要回答