我正在尝试遍历每10秒刷新一次的列表。
这是我尝试过的:
driver.get("https://www.winmasters.ro/ro/live-betting/")
events = driver.find_elements_by_css_selector('.event-wrapper.v1.event-live.odds-hidden.event-sport-1')
for i in range(len(events)):
try:
event = events[i]
name = event.find_element_by_css_selector('.event-details-team-name.event-details-team-a')# the error occurs here
except: # NoSuchElementException or StaleElementReferenceException
time.sleep(3) # i have tried up to 20 sec
event = events[i]
name = event.find_element_by_css_selecto('.event-details-team-name.event-details-team-a')
这不起作用,所以我尝试了另一个,除了
except: # second try that also did not work
element = WebDriverWait(driver, 20).until(
EC.presence_of_all_elements_located((By.CSS_SELECTOR, '.event-details-team-name.event-details-team-a'))
)
name = event.find_element_by_css_selecto('.event-details-team-name.event-details-team-a')
现在,我为name
分配了一些我永远不会使用的东西,例如:
try:
event = events[i]
name = event.find_element_by_css_selector('.event-details-team-name.event-details-team-a')
except:
name = "blablabla"
使用此代码,当页面刷新时,我会得到大约7或8个“ blablabla”,直到它再次从网页上找到我的选择器为止
答案 0 :(得分:1)
一个主要问题是您要先获取所有元素,然后遍历该列表。由于页面本身经常更新,因此您已经获取的元素已经“过时”,这意味着它们不再与当前的DOM对象相关联。当您尝试使用那些过时的元素时,Selenium会抛出StaleElementReferenceExceptions,因为它无法对那些过时的对象执行任何操作。
克服此问题的一种方法是仅在需要时正确获取和使用元素,而不是预先获取所有元素。我个人认为最干净的方法是使用CSS :nth-child()
方法:
from selenium import webdriver
def main():
base_css = '.event-wrapper.v1.event-live.odds-hidden.event-sport-1'
driver = webdriver.Chrome()
try:
driver.get("https://www.winmasters.ro/ro/live-betting/")
# Get a list of all elements
events = driver.find_elements_by_css_selector(base_css)
print("Found {} events".format(len(events)))
# Iterate through the list, keeping track of the index
# note that nth-child referencing begins at index 1, not 0
for index, _ in enumerate(events, 1):
name = driver.find_element_by_css_selector("{}:nth-child({}) {}".format(
base_css,
index,
'.event-details-team-name.event-details-team-a'
))
print(name.text)
finally:
driver.quit()
if __name__ == "__main__":
main()
如果运行上述脚本,则会得到以下输出:
$ python script.py
Found 2 events
Hapoel Haifa
FC Ashdod
现在,由于基础网页确实确实进行了很多更新,因此您仍然有很大机会出现SERE错误。为了解决这个问题,您可以使用重试装饰器(pip install retry
来获取包)来处理SERE并重新获取元素:
import retry
from selenium import webdriver
from selenium.common.exceptions import StaleElementReferenceException
@retry.retry(StaleElementReferenceException, tries=3)
def get_name(driver, selector):
elem = driver.find_element_by_css_selector(selector)
return elem.text
def main():
base_css = '.event-wrapper.v1.event-live.odds-hidden.event-sport-1'
driver = webdriver.Chrome()
try:
driver.get("https://www.winmasters.ro/ro/live-betting/")
events = driver.find_elements_by_css_selector(base_css)
print("Found {} events".format(len(events)))
for index, _ in enumerate(events, 1):
name = get_name(
driver,
"{}:nth-child({}) {}".format(
base_css,
index,
'.event-details-team-name.event-details-team-a'
)
)
print(name)
finally:
driver.quit()
if __name__ == "__main__":
main()
现在,尽管有上述示例,但我认为您的CSS选择器仍然存在问题,这是NoSuchElement异常的主要原因。如果没有更好地描述您实际上要使用此脚本完成的工作,我将无济于事。
答案 1 :(得分:1)
您可以使用JavaScript获取所有必需的数据。
下面的代码将为您提供事件map
的列表,其中包含所有详细信息,并且没有NoSuchElementException
或StaleElementReferenceException
错误:
me_id :唯一标识符
href :包含详细信息的href,可用于获取详细信息
team_a :一线队的名字
team_a_score :一队得分
team_b :第二支队伍的名称
team_b_score :第二队的得分
event_status :事件的状态
event_clock :事件的时间
def events = driver.execute_script('return [...document.querySelectorAll(\'[data-uat="live-betting-overview-leagues"] .events-for-league .event-live\')].map(e=>{return {me_id:e.getAttribute("me_id"), href:e.querySelector("a.event-details-live").href, team_a:e.querySelector(".event-details-team-a").textContent, team_a_score:e.querySelector(".event-details-score-1").textContent, team_b:e.querySelector(".event-details-team-b").textContent, team_b_score:e.querySelector(".event-details-score-2").textContent, event_status:e.querySelector(\'[data-uat="event-status"]\').textContent, event_clock:e.querySelector(\'[data-uat="event-clock"]\').textContent}})')
for event in events:
print(event.get('me_id'))
print(event.get('href')) #using href you can open event details using: driver.get(event.get('href'))
print(event.get('team_a'))
print(event.get('team_a_score'))
print(event.get('team_b'))
print(event.get('team_b_score'))
print(event.get('event_status'))
print(event.get('event_clock'))