【问题标题】:ActionChains Selenium Python doesn't work correctlyActionChains Selenium Python 无法正常工作
【发布时间】:2020-03-18 21:13:29
【问题描述】:

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

首先是 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
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)

但是如果我尝试直接在课堂上写它,它会起作用

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 selenium frontend


    【解决方案1】:

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

    A class definition is an executable statement. 
    

    (在class-definitions 中查看更多信息)

    基本上,python 运行类定义中的语句。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-18
      • 2018-07-02
      • 2016-08-01
      相关资源
      最近更新 更多