Reputation: 1690
I am using Selenium webdriver to write text using the following code
def typing(self, text):
time.sleep(1)
for character in text:
actions = ActionChains(self.driver)
actions.send_keys(character)
actions.perform()
Basically what it does is that it inputs some text and writes it, but I have to add a special characteristic: When characters "@"
or "&"
are present it has to count the next words until it finds a space " "
, and just before it, press enter key.
In this case I configured a function press_enter()
so this part will be easy.
Some examples:
@eat_pizza(press enter here) every day
I think &guacamole(press enter here) is good for health
&my_wife_Charlotte(press enter here) is so nice @fantastic(press enter here) and amazing
Upvotes: 1
Views: 360
Reputation: 4869
You can try below code
new_text = []
text = 'I think &guacamole is good for health'.split(' ')
# text is ['I', 'think', '&guacamole', 'is', 'good', 'for', 'health']
for word in text:
if word.startswith('@') or word.startswith('&'):
word += u' \ue007'
new_text.append(word)
# new_text is ['I', 'think', u'&guacamole \ue007', 'is', 'good', 'for', 'health']
new_text = ' '.join(new_text)
# new_text is u'I think &guacamole \ue007 is good for health'
Your method will now look like this
def typing(self, text):
time.sleep(1)
actions = ActionChains(self.driver)
actions.send_keys(text)
actions.perform()
You can send the whole text at the time and each time on u' \ue007'
(the same as Keys.ENTER
) Enter should be triggered
UPDATE
new_text = []
text = 'I think &guacamole is good for health'.split(' ')
# text is ['I', 'think', '&guacamole', 'is', 'good', 'for', 'health']
for word in text:
new_text.append(word)
if word.startswith('@') or word.startswith('&'):
new_text.append(press_enter)
def typing(self, text):
time.sleep(1)
actions = ActionChains(self.driver)
for word in new_text:
if not isinstance(word, str):
word()
else:
actions.send_keys(text)
actions.perform()
Upvotes: 1