【Python】selenium遇到ActionChains.move_to_element() takes 2 positional arguments but 4 were given报错

刚开始学的时候,用火绒的selenium插件录制了一个操作,导出为了python文件;

使用如下的命令运行这个文件(如果没有pytest用pip安装一下就行)

pip install pytest
pip install selenium

用如下命令执行导出的文件

pytest test_test01.py

会得到这个报错

      actions = ActionChains(self.driver)
>     actions.move_to_element(element, 0, 0).perform()
E     TypeError: ActionChains.move_to_element() takes 2 positional arguments but 4 were given

test_test1.py:42: TypeError

可我压根没有修改过导出的文件,这个报错是怎么来的呢?

目测是高低版本的python的selenium包中,报错的ActionChains.move_to_element()这个函数的传参被修改过;而插件生成的代码中,上方的这个就是正确的传参,下方的这个带0,0的传参是错误的

    # 3 | mouseOver | css=.row:nth-child(5) > .col-sm-3:nth-child(3) > .xe-widget | 
    element = self.driver.find_element(By.CSS_SELECTOR, ".row:nth-child(5) > .col-sm-3:nth-child(3) > .xe-widget")
    actions = ActionChains(self.driver)
    actions.move_to_element(element).perform()  # 正确
    # 4 | mouseOut | css=.row:nth-child(5) > .col-sm-3:nth-child(3) > .xe-widget | 
    element = self.driver.find_element(By.CSS_SELECTOR, "body")
    actions = ActionChains(self.driver)
    actions.move_to_element(element, 0, 0).perform() # 错误

image-20230815130034250

可以查看函数定义,只接受一个参数

image-20230815130102297

把这里的两个0,0删除掉就行了,运行的结果和我录制的效果相同,没有问题!

image-20230815130134022

顺带附上本次debug的版本号

python             3.10.5
pytest             7.4.0
selenium           4.11.2
火狐浏览器版本 116.0.2(64位)
火狐浏览器Selenium IDE插件版本 3.17.4

如果帮到了你,还请评论支持我一下,谢谢!

猜你喜欢

转载自blog.csdn.net/muxuen/article/details/132296695
今日推荐