【发布时间】: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)
有人能解释一下为什么吗? , 如果我只想用第一种方式写,我应该怎么做才能让它工作? .非常感谢您的帮助
【问题讨论】: