错误: Message: stale element reference: element is not attached to the page document
下面是代码
from selenium import webdriver
class douyu(object):
def __init__(self):
self.url = "https://www.douyu.com/g_LOL"
self.driver = webdriver.Chrome()
def parse_data(self):
room_list = self.driver.find_elements_by_xpath('//li[@class="layout-Cover-item"]/div')
#获取节点数据
for room in room_list:
temp = {}
temp['title'] = room.find_element_by_xpath('./a/div[2]/div[1]/h3').text
temp['type'] = room.find_element_by_xpath('./a/div[2]/div[1]/span').text
temp['boss'] = room.find_element_by_xpath('./a/div[2]/div[2]/h2/div').text
temp['num'] = room.find_element_by_xpath('./a/div[2]/div[2]/span').text
print(temp)
def run(self):
#url
#dirver
#get
self.driver.get(self.url)
#parse
self.parse_data()
#save
#next
if __name__ == '__main__':
Douyu = douyu()
Douyu.run()
下面是报错
{'title': '峡谷之巅1100分冲前十,双倍亲密度', 'type': '英雄联盟', 'boss': '凛然丶丶丶', 'num': '51万'}
{'title': '石页:峡谷之巅小小号90胜率冲王者!', 'type': '英雄联盟', 'boss': '石页的第一根矛s', 'num': '100.8万'}
{'title': '17点LPL春季赛V5vsFPX', 'type': '英雄联盟', 'boss': '英雄联盟赛事', 'num': '193.2万'}
{'title': '孙悟空:新版教学,版本答案', 'type': '英雄联盟', 'boss': '孙悟空丨兰林汉', 'num': '173.3万'}
{'title': '神子之手三星5费king挑战中!', 'type': 'lol云顶之弈', 'boss': '阿涛皎月Carry', 'num': '115.9万'}
{'title': '今天双倍亲密度!!', 'type': '英雄联盟', 'boss': '小景呀ovo', 'num': '68.2万'}
{'title': '微笑:峡谷上分!', 'type': '英雄联盟', 'boss': '微笑', 'num': '131.7万'}
{'title': '峡谷安静点玩哦', 'type': '英雄联盟', 'boss': 'impOUO', 'num': '121.5万'}
{'title': '峡谷之巅新号冲分 3567314', 'type': '英雄联盟', 'boss': '王纪超666', 'num': '119.9万'}
{'title': '黑色玫瑰通宵有车位~~~~', 'type': '英雄联盟', 'boss': '觅心女王丶游游', 'num': '7.2万'}
{'title': '瓜皮薇:黑色玫瑰一起玩啊', 'type': '英雄联盟', 'boss': '一只阿薇', 'num': '14.5万'}
{'title': '琪亚娜教学 5526219', 'type': '英雄联盟', 'boss': '南波儿大魔王丶', 'num': '184.8万'}
{'title': '开播了,来啊来啊', 'type': '英雄联盟', 'boss': '芜湖大司马丶', 'num': '188万'}
{'title': '云顶起飞!', 'type': '英雄联盟', 'boss': '余小C真的很强', 'num': '171.8万'}
{'title': '淑怡:来了嗷', 'type': '英雄联盟', 'boss': '不2不叫周淑怡', 'num': '282.1万'}
{'title': '银川第一寡妇', 'type': '英雄联盟', 'boss': '银川第一寡妇', 'num': '7378'}
Traceback (most recent call last):
File "E:/python/Githubup/douyu.py", line 31, in <module>
Douyu.run()
File "E:/python/Githubup/douyu.py", line 26, in run
self.parse_data()
File "E:/python/Githubup/douyu.py", line 14, in parse_data
temp['type'] = room.find_element_by_xpath('./a/div[2]/div[1]/span').text
File "D:\python3.7.4\lib\site-packages\selenium\webdriver\remote\webelement.py", line 76, in text
return self._execute(Command.GET_ELEMENT_TEXT)['value']
File "D:\python3.7.4\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "D:\python3.7.4\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "D:\python3.7.4\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
(Session info: chrome=89.0.4389.90)
(Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.17763 x86_64)
Process finished with exit code 1