Reputation: 1690
I am using Selenium webdriver to write text with some special features in the webdriver using the following code:
def typing_(self, text):
time.sleep(1)
new_text = []
text_ = text.split(' ')
for word in text_:
new_text.append(word + ' ')
if word.startswith('@') or word.startswith('&'):
new_text.append(self.enter_key)
# new_text is the result of parsing text and adding the function
time.sleep(1)
actions = ActionChains(self.driver)
for word_ in new_text:
if not isinstance(word_, str):
word_()
time.sleep(1)
else:
for char in word_:
actions.send_keys(char[0])
actions.perform()
time.sleep(1)
Basically what it does is that it inputs some text and writes it,
I have also added a special characteristic: When characters "@" or "&" are present it counts the next characters until it finds a space " ", and just before it, presses enter key.
In this case I configured a function press_enter() so this part will be easy.
Example:
I think @guacamole(press enter here) is good for health
When I send the text to the webdriver, the result I am getting is the following: NOTE: It actually presses enter key in the right place
II I tI thI thiI thinI thinkI think I think @I think @gI think @guI think @guaI think @guacI think @guacaI think @guacamI think @guacamoI think @guacamolI think @guacamoleI think @guacamole
I think @guacamole iI think @guacamole isI think @guacamole is I think @guacamole is gI think @guacamole is goI think @guacamole is gooI think @guacamole is goodI think @guacamole is good I think @guacamole is good fI think @guacamole is good foI think @guacamole is good forI think @guacamole is good for I think @guacamole is good for hI think @guacamole is good for heI think @guacamole is good for heaI think @guacamole is good for healI think @guacamole is good for healtI think @guacamole is good for healthI think @guacamole is good for health
Upvotes: 0
Views: 88
Reputation: 1
def typing_(self, text):
import time
from selenium.webdriver.common.action_chains import ActionChains
# Introduce a small initial delay for better UI sync
time.sleep(0.5)
# Split the text into words
words = text.split(' ')
processed_text = []
# Process each word
for word in words:
processed_text.append(word + ' ') # Add the word with trailing space
if word.startswith('@') or word.startswith('&'):
processed_text.append(self.enter_key) # Append enter_key if conditions match
# Initialize ActionChains
actions = ActionChains(self.driver)
# Iterate through the processed text
for item in processed_text:
if callable(item): # If it's a function (like self.enter_key), execute it
item()
time.sleep(0.5) # Small delay after function execution
else: # If it's a string, type it character by character
for char in item:
actions.send_keys(char) # Add the character to ActionChains
time.sleep(0.1) # Small delay for each character
actions.perform() # Perform the action batch
actions.reset_actions() # Reset for the next batch
# Final delay to ensure typing completes
time.sleep(0.5)
try above code..
Upvotes: 0
Reputation: 19979
def typing_(self, text):
time.sleep(1)
new_text = []
text_ = text.split(' ')
for word in text_:
new_text.append(word + ' ')
if word.startswith('@') or word.startswith('&'):
new_text.append(self.enter_key)
# new_text is the result of parsing text and adding the function
time.sleep(1)
actions = ActionChains(self.driver)
for word_ in new_text:
if not isinstance(word_, str):
word_()
time.sleep(1)
else:
for char in word_:
actions.send_keys(char[0])
actions.perform()
actions.reset_actions()
time.sleep(1)
Try adding reset_actions in forloop else the action will chain the previous send key action
If that doesn't work then use any of two solution:
first move action initialization to inside of loop:
for char in word:
actions = webdriver.ActionChains(driver)
actions.send_keys(char[0])
actions.perform()
time.sleep(1)
** updated seelnium to alpha v4**
pip install selenium==4.0.0.a7
https://github.com/SeleniumHQ/selenium/issues/6837
reset_actions() is not reseting the actions.
There was a bug for this and it was fixed in latest seleniumv4
in selenium v3 you can use the above mention fix or
for char in word:
actions.send_keys(char[0])
time.sleep(1)
actions.perform()
actions.w3c_actions.clear_actions()
for device in actions.w3c_actions.devices:
device.clear_actions()
Upvotes: 1