selenium AttributeError:‘List’对象没有属性‘Find_Element’

ac1kyiln  于 2022-11-10  发布在  其他
关注(0)|答案(1)|浏览(326)
AttributeError                            Traceback (most recent call last)
Input In [200], in <cell line: 17>()
     15 sleep(3)
     17 for match in matches:
---> 18     team = matches.find_element(By.XPATH,('//span[@class="team"]')).text
     19     scores = matches.find_element(By.XPATH,('//span[@class="score"]')).text
     20     print(team,scores)

AttributeError: 'list' object has no attribute 'find_element'

大家好,我在我的代码中有这个属性错误,我试图用FIND_ELEMENTS替换FIND_ELEMENT,但它不起作用,所以我可以做什么来实现我的循环和取消在官方网站Premiere联盟足球上的2020/2021年SAISO2020/2021年的每一场比赛
我的代码如下:

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
website = "https://www.premierleague.com/results?co=1&se=363&cl=-1"
driver.get(website)
sleep(10)
element = driver.find_element(By.XPATH,('//*[@class="_24Il51SkQ29P1pCkJOUO-7"]/button')).click()
sleep(10)
element2 = driver.find_element(By.XPATH,('//a[@id="advertClose"]'))
sleep(10)
driver.execute_script("arguments[0].click();",element2);

# WebDriverWait(driver, 10).until(driver.find_element(By.ID,('//*[@id="advertClose"]')).click()

matches = driver.find_elements(By.XPATH,'//ul[@class="matchList"]//li')

sleep(3)

for match in matches:
    team = matches.find_element(By.XPATH,('//span[@class="team"]')).text
    scores = matches.find_element(By.XPATH,('//span[@class="score"]')).text
    print(team,scores)

事先感谢你的帮助

a11xaf1n

a11xaf1n1#

问题就在这里:

for match in matches:
team =**matches**.find_element(By.XPATH,('//span[@class="team"]')).text
scores =**matches**.find_element(By.XPATH,('//span[@class="score"]')).text
print(team,scores)

您循环遍历匹配项,而不是编写match.find_elements,而是编写matches.find_element。(这是您当前正在循环的列表)

相关问题