首页 > 解决方案 > 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()

标签: pythonseleniumselenium-webdriver

解决方案


我猜你的错误是你正在使用find_element_by_xpath而不是find_elements_by_xpathin element = driver.find_element_by_xpath("//tr[@class='rowLive']//a/@href")line 所以这实际上返回一个元素,而你参加了那里的所有链接。
所以目前html_content是单个元素的 HTML。和容器
也是如此。souptable


推荐阅读