我对Selenium和Python都是新手,我对ActionChains有一个问题,我不明白。我想用ActionChain点击一个元素并将它移动到另一个元素,我尝试了两种方法。你知道吗
首先,两个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)
有人能解释一下为什么吗?,如果我只想用第一种方式写它,我应该怎么做才能让它工作呢。我非常感谢你的帮助
第二种方法“有效”是因为
(详见class-definitions)
基本上python在类定义中运行语句。你知道吗
如果您想使用第一种方法(假设您想使用unittest),也许您可以将firsttest方法定义为test\uxyz(self):。。。最后你可以打电话单元测试.main(),类似于basic example。你知道吗
相关问题 更多 >
编程相关推荐