首页 > 解决方案 > 为什么我收到 TypeError:'ActionChains' 对象不可迭代

问题描述

当我运行下面的代码时,我收到错误“TypeError: 'ActionChains' object is not iterable”。我究竟做错了什么?

回溯(最近一次通话最后):文件“/Users/Albert/Automation/online-tests/drawingTest.py”,第 55 行,在 test_onlineTest self.test_drawGreenPencil() 文件“/Users/Albert/Automation/online-tests/drawingTest .py",第 147 行,在 test_drawGreenPencil self.draw_lines(action, [(60, -30), (40, 30), (-40, 30), (-60, -30)]) 文件 "/Users/ Albert/Automation/online-tests/drawingTest.py",第 59 行,draw_lines 中的 o 偏移量:TypeError:'ActionChains' 对象不可迭代

这是代码片段:

def draw_lines(action, offsets, scale=1):
    for o in offsets:
        action.click_and_hold()
        action.move_by_offset(scale * o[0], scale * o[1])
        action.release().perform()




def test_drawGreenPencil(self):
    #click free hand pencil icon
    self.driver.find_element(*MainPageLocators.freeHandPencil).click()
    print("\n" + "Click free hand pencil icon...OK")
    # click line or pen color picker icon
    self.driver.find_element(*MainPageLocators.penColorPicker).click()
    # click the green color from the color pallete sub menu
    self.driver.find_element_by_css_selector(
        ".line-colors [data-color='brown']").click()
    time.sleep(2)

    print ("Start drawing...")
    # Start mouse interactions
    action = ActionChains(self.driver)
    action.move_to_element(self.driver.find_element_by_css_selector('canvas.upper-canvas')).perform()


    self.draw_lines(action, [(-30, 60), (30, 40), (30, -40), (-30, -60)])
    self.draw_lines(action, [(60, -30), (40, 30), (-40, 30), (-60, -30)])
    self.draw_lines(action, [(-30, -60), (30, -40), (30, 40), (-30, 60)])
    self.draw_lines(action, [(-60, -30), (-40, 30), (40, 30), (60, -30)])

    self.draw_lines(action, [(-30, 60), (30, 40), (30, -40), (-30, -60)], 0.8)
    self.draw_lines(action, [(60, -30), (40, 30), (-40, 30), (-60, -30)], 0.8)
    self.draw_lines(action, [(-30, -60), (30, -40), (30, 40), (-30, 60)], 0.8)
    self.draw_lines(action, [(-60, -30), (-40, 30), (40, 30), (60, -30)], 0.8)

    self.draw_lines(action, [(-30, 60), (30, 40), (30, -40), (-30, -60)], 0.6)
    self.draw_lines(action, [(60, -30), (40, 30), (-40, 30), (-60, -30)], 0.6)
    self.draw_lines(action, [(-30, -60), (30, -40), (30, 40), (-30, 60)], 0.6)
    self.draw_lines(action, [(-60, -30), (-40, 30), (40, 30), (60, -30)], 0.6)

    print ("Done.")
    time.sleep(20)

标签: python-3.xselenium-webdriverhtml5-canvas

解决方案


您已经执行了该操作。请重新创建它。

def draw_lines(self, offsets, scale=1):
    for o in offsets:
        action = ActionChains(self.driver)
        action.click_and_hold()
        action.move_by_offset(scale * o[0], scale * o[1])
        action.release().perform()

def test_drawGreenPencil(self):
    ...
    self.draw_lines(self, [(-30, 60), (30, 40), (30, -40), (-30, -60)])

推荐阅读