![](/img/trans.png)
[英]AttributeError: 'WebDriver' object has no attribute 'find_elements_by_xpath' in robotframework
[英]'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_name
、 find_element_by_xpath
、 find_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.