【环境信息】
python3.6+Selenium3.0.2+Firefox50.0+win7
【ActionChains类鼠标事件的常用方法】
1、右击:context_click()
2、双击:double_click()
3、拖动:drag_and_drop()
4、鼠标悬停在一个元素上:move_to_element()
5、按下鼠标左键在一个元素上:click_and_hold()
【问题】
在调用perform()方法时报错,不知道是不是环境配置问题,调用ActionChains中的click方法或者把perfom中的action方法去掉后不会报错,但是达不到效果。
报错信息:
Error Traceback (most recent call last): File "D:UserschenlePycharmProjectsuntitled estMouseOperate.py", line 57, in test_rightClick ActionChains(self.driver).context_click(username).perform() File "C:Program FilesPython36libsite-packagesseleniumwebdrivercommonaction_chains.py", line 74, in perform action() File "C:Program FilesPython36libsite-packagesseleniumwebdrivercommonaction_chains.py", line 225, in <lambda> Command.MOVE_TO, {'element': to_element.id})) File "C:Program FilesPython36libsite-packagesseleniumwebdriver emotewebdriver.py", line 236, in execute self.error_handler.check_response(response) File "C:Program FilesPython36libsite-packagesseleniumwebdriver emoteerrorhandler.py", line 192, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: POST /session/32d7ba0e-d8b9-4ad2-8c8f-843f9f627b28/moveto did not match a known command
代码:
def testRightClick(self): self.MousOperate() #定位右击的元素,并且多选元素进行右击操作 elements = self.driver.find_element_by_xpath("//div[@class='list-view']/dd[1]/div[2]/div[2]/div") elements = self.driver.find_element_by_xpath("//div[@id='layoutMain']") #print(elements.get_attribute('class')) time.sleep(2) ActionChains(self.driver).context_click(elements).perform() time.sleep(5)