在selenium python中循环span元素,AttributeError:';列表';对象没有属性';单击';
我想循环浏览一个跨度元素列表,并让Selenium单击所有可用的跨度元素。目前我得到一个AttributeError:“list”对象没有属性“click”。有关更多详细信息,请参见下面的代码片段 我创建了一个作为超类的BasePage,其中定义了我的大多数selenium方法:在selenium python中循环span元素,AttributeError:';列表';对象没有属性';单击';,python,loops,selenium,object,model,Python,Loops,Selenium,Object,Model,我想循环浏览一个跨度元素列表,并让Selenium单击所有可用的跨度元素。目前我得到一个AttributeError:“list”对象没有属性“click”。有关更多详细信息,请参见下面的代码片段 我创建了一个作为超类的BasePage,其中定义了我的大多数selenium方法: from selenium.common.exceptions import NoSuchElementException, TimeoutException from selenium.webdriver.suppo
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()
我还创建了一个页面对象,在其中定义了页面的所有定位器和操作
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选择器:
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.\u find\u all(self,locator)
返回一个元素列表,因此不要将其用作
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
,因此\u click\u all()
可能会应用于在静态页面上执行某些操作的元素列表
更新
这是您的问题:方法
self.\u find\u all(self,locator)
返回一个元素列表,因此不要将其用作
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
,因此\u click\u all()
可能会应用于在静态页面上执行某些操作的元素列表
更新
现在测试通过了。非常感谢你的帮助。但它只单击了第一个元素。所以我的逻辑是不正确的。读完你的最后一段。这对我来说很有意义。我的想法是在单击第一个元素后调用driver.back()方法,然后返回,单击第二个元素等,这样做行不通。当一个页面导航到新页面时,我如何实现我的想法?检查更新的回答当前我得到“索引器:列表索引超出范围”尝试更新的回答哇,非常感谢。这起作用了。非常感谢你的帮助。仍然有大量python和selenium需要学习,但很高兴我现在可以自动化工作中最重复的任务。这将节省我很多时间。谢谢。好了,现在考试通过了。非常感谢你的帮助。但它只单击了第一个元素。所以我的逻辑是不正确的。读完你的最后一段。这对我来说很有意义。我的想法是在单击第一个元素后调用driver.back()方法,然后返回,单击第二个元素等,这样做行不通。当一个页面导航到新页面时,我如何实现我的想法?检查更新的回答当前我得到“索引器:列表索引超出范围”尝试更新的回答哇,非常感谢。这起作用了。非常感谢你的帮助。仍然有大量python和selenium需要学习,但很高兴我现在可以自动化工作中最重复的任务。这将节省我很多时间。谢谢。