[Python] selenium encounters ActionChains.move_to_element() takes 2 positional arguments but 4 were given and reports an error

When I first started learning, I used Tinder’s selenium plug-in to record an operation and exported it to a python file;

Use the following command to run this file (if you don’t have pytest, just install it with pip)

pip install pytest
pip install selenium

Execute the exported file with the following command

pytest test_test01.py

You will get this error

      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

But I haven't modified the exported file at all. Where did this error come from?

Visual inspection is that in the selenium package of high and low versions of Python, the parameters ActionChains.move_to_element()of the function that reported the error have been modified; and in the code generated by the plug-in, the upper one is the correct parameter, and the lower one 0,0is wrong.

    # 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

You can view the function definition, which only accepts one parameter

image-20230815130102297

Just delete the two here 0,0. The running result is the same as what I recorded, no problem!

image-20230815130134022

By the way, the version number of this debug is attached.

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

If it helped you, please leave a comment to support me, thank you!

Guess you like

Origin blog.csdn.net/muxuen/article/details/132296695