selenium.common.exceptions.WebDriverException:Message:TypeError:node.ownerDocument为空

2024-09-24 22:30:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我想了解这个失败的原因,以及我应该在我的脚本中修改什么来解决这个问题,并能够提供所需的抓取。遵循以下数据:

试验地点:

http://sports.williamhill.com/bet/pt/betlive/24

我正在尝试收集这些框中的链接:

enter image description here

find_element_by_xpath是正确的,但是当我尝试运行脚本时,它返回错误:

selenium.common.exceptions.WebDriverException: Message: TypeError: node.ownerDocument is null

虚拟工作室代码中的我的脚本项目:

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()


Tags: fromimport脚本comhttphtmldriverselenium
1条回答
网友
1楼 · 发布于 2024-09-24 22:30:14

我猜您的错误在于您在element = driver.find_element_by_xpath("//tr[@class='rowLive']//a/@href")行中使用了find_element_by_xpath而不是find_elements_by_xpath,因此这实际上返回了一个元素,而您需要在那里获取所有链接。
所以目前html_content是一个单一元素的HTML。
对于souptable容器也是如此

相关问题 更多 >