遍历硒python中的span元素,AttributeError:'list'对象没有属性'click' [英] Loop through span elements in selenium python, AttributeError: 'list' object has no attribute 'click'

查看:256
本文介绍了遍历硒python中的span元素,AttributeError:'list'对象没有属性'click'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想遍历整个span元素并使Selenium单击所有可用的span元素.当前,我得到一个AttributeError:'list'对象没有属性'click'.有关更多详细信息,请参见下面的代码段.

I want to loop through a list of span elements and make Selenium click all span elements available. Currently I'm getting an AttributeError: 'list' object has no attribute 'click'. See code snippets below for more details.

我将BasePage作为超类,在其中定义了大多数硒方法:

I made have a BasePage as super class where I define most of my selenium methods:

from selenium.common.exceptions import NoSuchElementException, TimeoutException
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait


class BasePage(object):

    def __init__(self, driver):
        self.driver = driver

    def _visit(self, url):
        self.driver.get(url)

    def _find(self, locator):
        return self.driver.find_element(locator["by"], locator["value"])

    def _find_all(self, locator):
        return self.driver.find_elements(locator["by"], locator["value"])

    def _click(self, locator):
        self._find(locator).click()

    def _click_all(self, locator):
        self._find_all(locator).click()

    def _type(self, locator, input_text):
        self._find(locator).send_keys(input_text)

    def _clear(self, locator):
        self._find(locator).clear()

我还制作了一个页面对象,用于定义页面的所有定位符和动作.

I have also made a page object where I define all the locators and actions of a page.

from selenium.webdriver.common.by import By

from .base_page import BasePage


class RelatiePage(BasePage):

    _open_actieve_polis = {"by": By.CSS_SELECTOR, "value": "td:nth-
    child(2)"}

    def __init__(self, driver):
        super(BasePage, self).__init__()
        self.driver = driver


    def relatie_tabs_(self):
        self._click(self._open_actieve_polissen_tab)
        self._click_all(self._open_actieve_polis)
        self.driver.back()

这些是我想循环浏览的html选择器:

these are the html selectors i want to loop through:

tbody > tr:nth-child(2) > td:nth-child(2) > span > a
tbody > tr:nth-child(3) > td:nth-child(2) > span > a
tbody > tr:nth-child(4) > td:nth-child(2) > span > a
tbody > tr:nth-child(5) > td:nth-child(2) > span > a
tbody > tr:nth-child(6) > td:nth-child(2) > span > a

我当前收到的错误:

line 46, in relatie_tabs_
    self._click_all(self._open_actieve_polis), self.driver.back()

 line 24, in _click_all
    self._find_all(locator).click()

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

推荐答案

这是您的问题:方法self._find_all(self, locator)返回一个元素列表,所以不要将其用作

Here is your problem: method self._find_all(self, locator) returns a list of elements, so instead of use it as

def _click_all(self, locator):
    self._find_all(locator).click()

您应该

def _click_all(self, locator):
    for element in self._find_all(locator):
        element.click()

还请注意,如果单击目标元素触发页面刷新/导航到新页面,您将得到StaleElementReferenceException,因此_click_all()可能会应用于在静态页面上执行某些操作的元素列表

Also note that if clicking target element triggers page refresh/navigation to new page, you will get StaleElementReferenceException, so _click_all() might be applied to list of elements that performs some actions on static page

更新

from selenium.webdriver.support.ui import WebDriverWait as wait

def _click_all(self, locator):
    counter = len(self._find_all(locator))
    for index in range(counter):
        self._find_all(locator)[index].click()
        self.driver.back()
        wait(self.driver, 10).until(lambda driver: len(self._find_all(locator)) == counter)

这篇关于遍历硒python中的span元素,AttributeError:'list'对象没有属性'click'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆