ActionChains 中的 perform() 和 reset_actions() 不起作用 selenium python

Sus*_*was 7 python selenium

这是没有错误的代码:

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")
Run Code Online (Sandbox Code Playgroud)

我正在使用navigation()函数:

在循环区

actions.send_keys(Keys.TAB)

actions.reset_actions()

我需要重置操作,但它不会重置以前的 preform()

什么是击球手的方式来做到这一点。

请观看youtube视频以获得更清晰的理解。

Ser*_*ers 4

该问题已修复,但将在下一个版本中修复。检查github 中的问题#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()
Run Code Online (Sandbox Code Playgroud)