簡體   English   中英

'WebDriver' object 沒有屬性 'find_elements_by_xpath 文件“C:\Users\luigi\Downloads\script_sbs.py”,第 20 行

[英]'WebDriver' object has no attribute 'find_elements_by_xpath File "C:\Users\luigi\Downloads\script_sbs.py", line 20

import re import time from datetime import datetime from operator import itemgetter import openpyxl import pandas as pd from bs4 import BeautifulSoup from selenium import webdriver driver = webdriver.Chrome() driver.minimize_window() url = 'https://www.sbostats.com/partite' tgame = [] driver.get(url) tab = driver.find_element_by_xpath("/html/body/div[2]/div[3]/div/div/div[2]/app-root/div/app-matches/section") tab1 = tab.find_elements_by_tag_name('a')

現在不推薦使用find_element_by_namefind_element_by_xpathfind_element_by_id等所有方法。
您應該使用find_element(By.代替。
所以,而不是

tab = driver.find_element_by_xpath("/html/body/div[2]/div[3]/div/div/div[2]/app-root/div/app-matches/section")

應該是現在

tab = driver.find_element(By.XPATH, "/html/body/div[2]/div[3]/div/div/div[2]/app-root/div/app-matches/section")

同樣關於

tab1 = tab.find_elements_by_tag_name('a')

應該改成

tab1 = tab.find_elements(By.TAG_NAME, 'a')

你需要導入這個:

from selenium.webdriver.common.by import By

此外,您還必須改進定位器。
長的絕對 XPath 和 CSS 這樣的選擇器/html/body/div[2]/div[3]/div/div/div[2]/app-root/div/app-matches/section極易損壞且不可靠。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM