【问题标题】:perform() and reset_actions() in ActionChains not working selenium pythonActionChains 中的 perform() 和 reset_actions() 不起作用 selenium python
【发布时间】:2021-05-20 05:05:40
【问题描述】:

这是没有错误的代码:

perform() 和 reset_actions()

但这两个功能必须结合使用

import os
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
import random

# Setting the chrome_options
global chrome_options
chrome_options = Options()
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument('--profile-directory=Default')
prefs = {"profile.default_content_setting_values.notifications": 2}
chrome_options.add_experimental_option("prefs", prefs)
chrome_options.add_argument('disable-infobars')
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])

google_search = [
    "1.' driver.switch_to.active_element' ",
    "2.this code is a one of important snippet for facebook automation.",
]

random_google_search = random.choice(google_search)

# Setting the Chrome Driver
global driver
driver = webdriver.Chrome("chromedriver.exe", chrome_options=chrome_options)

# Setting the Actions
global actions
actions = ActionChains(driver)  

    
#the loop Running
def navigation():
    time.sleep(5)
    actions.reset_actions()

    driver.get("https://google.com")

    actions.send_keys(random_profile_post)

    total_tab = 3
    sleep_time = 1
    implicitly_wait_time = 4

    actions.reset_actions()
    driver.implicitly_wait(implicitly_wait_time)
    time.sleep(sleep_time)

    for i in range(total_tab):
        actions.send_keys(Keys.TAB)
        print("Pressing * " + str(i + 1) + " * No Tab")

    actions.send_keys(Keys.ENTER)
    actions.perform()


for i in range(10):
    navigation()
    print("Pressing * " + str(i + 1) + " * st navigation function")

我正在使用 navigation() 函数:

在循环区

actions.send_keys(Keys.TAB)

actions.reset_actions()

我需要重置操作,但它没有重新安装以前的 preform()

如何做到这一点。

Please watch the youtube video for more clear understanding.

【问题讨论】:

  • “我需要重置操作,但它没有重新安装以前的 preform()”——这是什么意思?你期待什么不会发生?
  • 在你的循环中你也有actions.send_keys(Keys.TAB) actions.reset_actions() - 一方面它看起来不像那个标签被执行过。
  • @C.Peck 它在完成循环时执行 actions.send_keys(Keys.ENTER) actions.perform()
  • 是的,这就是代码所说的对吗?我仍然不确定问题是什么
  • @C.Peck 请观看我重新录制并添加到帖子下方的视频

标签: python selenium


【解决方案1】:

该问题已修复,但将在下一个版本中修复。查看github中的issue#6837。

现在你可以使用临时解决方案。

def perform_actions():
    """ Perform and reset actions """
    actions.perform()
    actions.reset_actions()
    for device in actions.w3c_actions.devices:
        device.clear_actions()


# the loop Running
def navigation():
    time.sleep(5)

    driver.get("https://google.com")

    actions.send_keys("A")

    total_tab = 4
    sleep_time = 1
    implicitly_wait_time = 4

    # actions.reset_actions()
    driver.implicitly_wait(implicitly_wait_time)
    time.sleep(sleep_time)

    for i in range(total_tab):
        actions.send_keys(Keys.TAB)
        print("Pressing * " + str(i + 1) + " * No Tab")

    actions.send_keys(Keys.ENTER)
    perform_actions()
    print()

【讨论】:

  • 什么是 w3c_action?
  • 您提到的 GitHub 问题来自 2019 年,已有 2 年多的历史。你能再看一遍吗?
  • 试用最新的 4.0.0.b3 版本。
  • I didn't find for device in actions.w3c_actions.devices: device.clear_actions() clear_actions() 在你找到它的洞文档中。
【解决方案2】:

你可以在函数内部实例化它。如果这不起作用,您可以尝试将其全部放入 def,除了应该删除的 global 行。

#the loop Running
def navigation():
   actions = ActionChains(driver) 
.
.
.

【讨论】:

  • 我很难判断拐角的确切位置,但是当循环中有 driver.get() 时,必须小心。请考虑在函数末尾添加 driver.quit()。
【解决方案3】:

您应该将 ActionChains 存储在一个变量中,这样您就可以将您的操作存储在一个对象中。

def navigateGroupPostBtn():
    navigateGroupJoinBtnActions = ActionChains(driver)
    total_tab = 23
    sleepTime = 1
    implicitlyWaitTime = 20

    time.sleep(sleepTime)
    for i in range(total_tab):
        driver.implicitly_wait(implicitlyWaitTime)
        navigateGroupJoinBtnActions.send_keys(Keys.TAB)
        print("Pressing * " + str(i + 1) + " * No Tab")
    navigateGroupJoinBtnActions.send_keys(Keys.ENTER)
    navigateGroupJoinBtnActions.perform_actions()
    print("Navigate Groum Join Btn Successfully ") 

navigateGroupJoinBtnActions = ActionChains(驱动程序)

【讨论】:

    猜你喜欢
    • 2018-12-28
    • 1970-01-01
    • 2014-10-02
    • 2020-03-18
    • 2021-12-26
    • 1970-01-01
    • 2021-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多