Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ActionChains不';我不能正常工作_Python_Selenium_Frontend - Fatal编程技术网

Python ActionChains不';我不能正常工作

Python ActionChains不';我不能正常工作,python,selenium,frontend,Python,Selenium,Frontend,我对Python的Selenium是新手,我对ActionChains有一个问题,我无法理解。我想用ActionChain单击一个元素并将其移动到另一个元素,我尝试了两种方法 首先,2个py文件的组合不起作用 import time from selenium.webdriver.common.action_chains import ActionChains def action_function(driver,start,des): time.sleep(2) Action

我对Python的Selenium是新手,我对ActionChains有一个问题,我无法理解。我想用ActionChain单击一个元素并将其移动到另一个元素,我尝试了两种方法

首先,2个py文件的组合不起作用

import time
from selenium.webdriver.common.action_chains import ActionChains

def action_function(driver,start,des):
    time.sleep(2)
    ActionChains(driver).click_and_hold(start).move_to_element(des).release().perform()
    time.sleep(3)
但是如果我试着在课堂上直接写的话,它是有效的

import time
import unittest
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

class PythonOrgSearch(unittest.TestCase):


    driver = webdriver.Firefox('./geckodriver') 
    driver.get('file:///C:/My-Project/Probe/index.html')

    time.sleep(5) # Let the user actually see something!
    dragitem = driver.find_element_by_id('mydivheader')
    print(dragitem.get_attribute('innerHTML'))
    time.sleep(5)
    destination = driver.find_element_by_id('destination')
    time.sleep(4)
    ActionChains(driver).click_and_hold(dragitem).move_to_element(destination).release().perform()
    time.sleep(3)
有人能解释一下原因吗,如果我只想用第一种方式写,我应该怎么做才能让它工作。我非常感谢您的帮助,因为第二种方法“有效”

(请参阅中的详细信息)

基本上python在类定义中运行语句

如果您想使用第一种方法(假设您想使用unittest),也许您可以将firsttest方法定义为test_xyz(self):。。。最后,您可以调用unittest.main(),类似于。

第二种方法“有效”,因为

(请参阅中的详细信息)

基本上python在类定义中运行语句

如果您想使用第一种方法(假设您想使用unittest),也许您可以将firsttest方法定义为test_xyz(self):。。。最后,您可以调用unittest.main(),类似于中的

import time
import unittest
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

class PythonOrgSearch(unittest.TestCase):


    driver = webdriver.Firefox('./geckodriver') 
    driver.get('file:///C:/My-Project/Probe/index.html')

    time.sleep(5) # Let the user actually see something!
    dragitem = driver.find_element_by_id('mydivheader')
    print(dragitem.get_attribute('innerHTML'))
    time.sleep(5)
    destination = driver.find_element_by_id('destination')
    time.sleep(4)
    ActionChains(driver).click_and_hold(dragitem).move_to_element(destination).release().perform()
    time.sleep(3)
A class definition is an executable statement.