Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perform() and reset_actions() in ActionChains not working selenium python

This is the code that habe no error:

perform() and reset_actions()

but these two functions have to work combinedly

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")

I am working with navigation() functions:

in the loop area

actions.send_keys(Keys.TAB)

actions.reset_actions()

I need to reset action but it's not reseating previous preform()

What will be the batter way to do that.

Please watch the youtube video for more clear understanding.

like image 563
Sushen Biswas Avatar asked May 20 '21 05:05

Sushen Biswas


People also ask

Is enabled function in selenium Python?

is_enabled() element method – Selenium Python with Examplesis_enabled method can check whether an html elemenet is enabled or disabled. You can write code that separates the code to be executed on the activated element and when the element is deactivated.

Is Selenium compatible with Python?

Selenium Scripts can be programmed using various languages such as JavaScript, Java, Python, etc.

What is the function of selenium in Python?

Selenium is an open source automation testing tool that supports a number of scripting languages like Python, C#, Java, Perl, Ruby, JavaScript, etc. depending on the application to be tested, one can choose the script accordingly.


Video Answer


3 Answers

The issue is fixed already but will be in the next releases. Check the issue #6837 in the github.

For now you can use temporary solution.

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()
like image 200
Sers Avatar answered Oct 21 '22 07:10

Sers


You can just instantiate it inside the function. If this does not work you can try to throw it all inside def, aside from the global lines, which should be deleted.

#the loop Running
def navigation():
   actions = ActionChains(driver) 
.
.
.
like image 34
Pedro Kaneto Suzuki Avatar answered Oct 21 '22 08:10

Pedro Kaneto Suzuki


You should store ActionChains in a variable, so you can store you action in a object.

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(driver)

like image 45
Jawad Avatar answered Oct 21 '22 08:10

Jawad