Selenium webdriver:发送密钥时出现不必要且重复的值

问题描述 投票:0回答:2

我正在使用 Selenium Webdriver 使用以下代码在 Webdriver 中编写具有一些特殊功能的文本:

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)

它的作用基本上就是输入一些文本并写入,

我还添加了一个特殊功能:当出现字符“@”或“&”时,它会计算下一个字符,直到找到空格“”,并在其之前按回车键。

在本例中,我配置了一个函数 press_enter(),因此这部分会很简单。

示例:

我认为@guacamole(按此处输入)对健康有益

当我将文本发送到网络驱动程序时,我得到的结果如下: 注意:它实际上是在正确的位置按回车键

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
python python-3.x selenium parsing selenium-webdriver
2个回答
1
投票
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)

尝试在for循环中添加reset_actions,否则该操作将链接之前的发送键操作

如果这不起作用,请使用以下两种解决方案中的任何一种:

首先将动作初始化移至循环内部:

for char in word:
    actions = webdriver.ActionChains(driver)
    actions.send_keys(char[0])
    actions.perform()
    time.sleep(1)

** 将 seelnium 更新为 alpha v4**

pip install selenium==4.0.0.a7

对于第二种解决方案,您的代码将按原样工作

更多信息:

https://github.com/SeleniumHQ/selenium/issues/6837

reset_actions() 不会重置操作。

这有一个错误,并在最新的 seleniumv4 中修复了

在 selenium v3 中,您可以使用上述修复或

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

0
投票

def打字_(自我,文本): 导入时间 从 selenium.webdriver.common.action_chains 导入 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)

尝试上面的代码..

© www.soinside.com 2019 - 2024. All rights reserved.