简体   繁体   中英

ActionChains Selenium Python doesn't work correctly

I'm new in Selenium with Python and I have a problem with ActionChains , that I couldn't understand.I want to click on an element and move it to another element with ActionChain , I tried 2 ways to do this .

Firstly the combination of 2 py-files , they don't work

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
from hilffunktionen import hilffunktion

class PythonOrgSearch(unittest.TestCase):


    driver = webdriver.Firefox('./geckodriver') 

    @classmethod
    def firsttest(self):
        self.driver.get('file:///C:/My-Project/Probe/index.html')

        time.sleep(5) # Let the user actually see something!
        dragitem = self.driver.find_element_by_id('mydivheader')
        print(dragitem.get_attribute('innerHTML'))
        time.sleep(5)
        destination = self.driver.find_element_by_id('destination')
        time.sleep(4)
        hilffunktion.action_function(self.driver,dragitem,destination)
        time.sleep(3)

But if I try to write it direct in class , it works

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)

Could someone explain me why ? , If I just want to write it in the first way , what should I do , so that it works ? . I would be very thankful for you help

I think you need to perform drag and drop functionality. please refer below code for the same

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

source = WebDriverWait(driver, 15).until(EC.visibility_of_element_located((By.XPATH, "Your locator")))
dest = WebDriverWait(driver, 15).until(EC.visibility_of_element_located((By.XPATH, "Your locator")))

ActionChains(driver).click_and_hold(source).move_to_element(dest).release(dest).perform()

The second way "works" because

A class definition is an executable statement. 

(see more in class-definitions )

Basically python runs the statements in class definition.

If you want to do the first way (assume that you want to use unittest), maybe you can define the firsttest method as test_xyz(self): ... and in the end you can call unittest.main(), similar in thebasic example .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM