python - ActionChains Selenium Python 无法正常工作
问题描述
我是使用 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
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)
有人可以解释一下为什么吗?, 如果我只想用第一种方式写它,我该怎么做才能让它工作?. 我会非常感谢你的帮助
解决方案
推荐阅读
- ruby - 使用 'Each' 方法在 do 循环中创建一个新数组
- javascript - Datatable JS 中的 API 数据
- php - MySQL 数据库中存储的值错误
- regex - 使用 DF、Grep、Awk 列出卷 | 重击壳
- javascript - Gatsby.js 中的 Polyfills - String.prototype.matchAll
- string - 如何将lua表中的条目视为字符串?
- aws-lambda - Traefik 具有到 ECS 后端的动态路由,作为一次性任务运行
- bash - BASH 停止没有错误,但如果在终端中复制则可以工作
- c# - 具有相同类型实体的多级级联删除
- python - 为什么我的 joblib 文件没有保存在与我的 Jupyter 文件相同的目录中?