简体   繁体   English

AttributeError: 'list' object 没有属性 'isDisplayed' Python Selenium

[英]AttributeError: 'list' object has no attribute 'isDisplayed' Python Selenium

I am trying to make a stock bot that checks if something is in stock and when I try to use:我正在尝试制作一个库存机器人来检查是否有库存以及何时尝试使用:

if ATC.isDisplayed():

I get the error:我得到错误:

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

My whole code:我的整个代码:

import time
import asyncio
import colorama
import subprocess
from colorama import Fore, Back, Style
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.expected_conditions import presence_of_element_located

driver = webdriver.Chrome(
    executable_path=r'C:\Users\Joe mama\Desktop\app\chromedriver.exe')

colorama.init(autoreset=True)

driver.get("https://www.currys.co.uk/gbuk/computing-accessories/components-upgrades/graphics-cards/msi-geforce-rtx-3060-ti-8-gb-gaming-x-trio-graphics-card-10219250-pdt.html")
driver.maximize_window()
Name = driver.find_elements_by_css_selector(
    "#content > div.product-page > section > div.to-print > h1 > span:nth-child(2)")
Price = driver.find_elements_by_css_selector(
    "#product-actions > div.amounts > div > div > span.ProductPriceBlock__Price-kTVxGg.QWqil")
Link = driver.find_elements_by_tag_name("a")
OOS = driver.find_elements_by_css_selector(
    "#product-actions > div.oos.oos-no-alt.border.space-b > strong")
ATC = driver.find_elements_by_css_selector(
    "#product-actions > div.channels.space-b > div.space-b.center > button")

while True:
    try:
        if OOS.isDisplayed():
            print(colorama.Fore.RED +
                  f"|| {Name[0].text} || Out Of Stock || {Price[0].text} ||")
            driver.refresh()
    except:
        if ATC.isDisplayed():
            print(colorama.Fore.GREEN +
                  f'|| {Name[0].text} || IN STOCK || {Price[0].text} ||')

If anyone could help that would be very helpful, I've been trying to figure this out for ages.如果有人可以提供帮助,那将非常有帮助,我多年来一直在努力解决这个问题。

ATC is of type list as find_elements_by_css_selector() would return a list . ATClist类型,因为find_elements_by_css_selector()会返回一个list

However, there is no method/attribute as isDisplayed() but is_displayed() and is applicable for WebElement only.但是,没有方法/属性 as isDisplayed()而是is_displayed()并且仅适用于WebElement


Solution解决方案

You need to iterate through the list of elements identified through:您需要遍历通过以下方式标识的元素列表:

OOS = driver.find_elements_by_css_selector("#product-actions > div.oos.oos-no-alt.border.space-b > strong")

possibly using a for() loop.可能使用for()循环。

This will help beginners and you would have made mistake in selecting the wrong function这将有助于初学者,你会在选择错误的 function 时犯错误

would have selected this one会选择这个

user_name = driver.find_elements_by_name()

you need to select this one你需要 select 这个

user_name = driver.find_element_by_name()

In simple words remove s from "elements"简单来说,从“元素”中删除s

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError: 'list' 对象没有使用 Selenium 和 Python 的属性 'click' - AttributeError: 'list' object has no attribute 'click' using Selenium and Python AttributeError: 'list' object 没有属性 'replace' Selenium Python - AttributeError: 'list' object has no attribute 'replace' Selenium Python Selenium AttributeError: 'list' object 没有属性 'text' - Selenium AttributeError: 'list' object has no attribute 'text' AttributeError:Selenium Webdriver中的python对象没有属性 - AttributeError: object has no attribute in python in selenium webdriver AttributeError:“ WebElement”对象没有属性(python)(硒) - AttributeError: 'WebElement' object has no attribute (python) (selenium) AttributeError:“列表”对象在python中没有属性“显示” - AttributeError: 'list' object has no attribute 'display' in python Python 2:AttributeError:“list”对象没有“strip”属性 - Python 2: AttributeError: 'list' object has no attribute 'strip' AttributeError: 'list' object 没有属性 'lower' - Python - AttributeError: 'list' object has no attribute 'lower' - Python AttributeError: 'list' object 在 Python 中没有属性 'values' - AttributeError: 'list' object has no attribute 'values' in Python AttributeError: 'list' object 没有属性 'items' - Python - AttributeError: 'list' object has no attribute 'items' - Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM