python - selenium.common.exceptions.WebDriverException:消息:TypeError:node.ownerDocument 为空
问题描述
我想了解此失败的原因以及我应该在脚本中修改什么以解决此问题并能够提供所需的抓取。关注数据:
测试地点:
http://sports.williamhill.com/bet/pt/betlive/24
我正在尝试收集这些框中的链接:
是正确的find_element_by_xpath
,但是当我尝试运行脚本时,它返回错误:
selenium.common.exceptions.WebDriverException: Message: TypeError: node.ownerDocument is null
我在 Virtual Studio 代码中的脚本项目:
import time
import requests
import pandas as pd
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
import json
url = "http://sports.williamhill.com/bet/pt/betlive/24"
option = Options()
option.headless = True
driver = webdriver.Firefox(options=option)
driver.get(url)
time.sleep(1)
element = driver.find_element_by_xpath("//tr[@class='rowLive']//a/@href")
html_content = element.get_attribute('outerHTML')
soup = BeautifulSoup(html_content, 'html.parser')
table = soup.find(name='href')
df_full = pd.read_html(str(table))[0]
importar = {}
importar['importando'] = df_full.to_dict('records')
print(importar['importando'])
driver.quit()
解决方案
我猜你的错误是你正在使用find_element_by_xpath
而不是find_elements_by_xpath
in element = driver.find_element_by_xpath("//tr[@class='rowLive']//a/@href")
line 所以这实际上返回一个元素,而你参加了那里的所有链接。
所以目前html_content
是单个元素的 HTML。和容器
也是如此。soup
table
推荐阅读
- reactjs - 如何将时间显示为 HH:MM 格式?
- java - Java中对Map的线程安全获取/清除操作的选择
- python - Change bind address on hug API
- python - Python 大约 128 个 if 语句
- puppeteer - Angular 8:找不到模块:错误:无法解析“ws”(Puppeteer)
- reactjs - React hooks:在清理调用期间访问设置器
- ios - 在本地通知上发送带有自定义声音的触觉
- swift - SwiftUI - 来自 Coordinator 的 ObservableObject 更新
- schema.org - JSON-LD:我怎样才能有多个 articleSection?
- c++ - 如何在 C++ 中显示当前的#pragma pack 设置?