在for循环python中集成时,actionchains send_keys发送的重复文本

时间:2019-06-30 03:22:13

标签: python python-3.x loops selenium selenium-webdriver

我尝试将send_keys发送到textarea,所以我使用动作链发送密钥。 我使用了这段代码:

url='https://translate.google.com/?hl=vi'
browserdriver.get(url)
list_test=['product description 1','product description 2']
for i in range (0,2):   
    try:
        body_text=list_test[i]
        browserdriver.execute_script("window.scrollTo(0, document.body.scrollHeight)")      
        item = WebDriverWait(browserdriver, 10).until(EC.presence_of_element_located((By.TAG_NAME, "textarea")))        
        actions.move_to_element(item).send_keys(body_text).perform()
        actions.reset_actions()
        time.sleep(1)
    except:
        pass
print("done")

然后将文本结果发送到Google翻译,如下所示:

product description 1product description 1product description 2

您可以看到它是如此奇怪,就像这样:

product description 1product description 2

我还在动作链的源代码中将print()插入到utils.py中,以了解将什么键入文本发送到send_keys函数:

def keys_to_typing(value):
    """Processes the values that will be typed in the element."""
    typing = []
    for val in value:
        if isinstance(val, Keys):
            typing.append(val)
        elif isinstance(val, int):
            val = str(val)
            for i in range(len(val)):
                typing.append(val[i])
        else:
            for i in range(len(val)):
                typing.append(val[i])    
    print(typing)#this is a code line that I inserted
    return typing

,keys_to_typing的输出控制台为:

['p', 'r', 'o', 'd', 'u', 'c', 't', ' ', 'd', 'e', 's', 'c', 'r', 'i', 'p', 't', 'i', 'o', 'n', ' ', '1']
['p', 'r', 'o', 'd', 'u', 'c', 't', ' ', 'd', 'e', 's', 'c', 'r', 'i', 'p', 't', 'i', 'o', 'n', ' ', '1']

['p', 'r', 'o', 'd', 'u', 'c', 't', ' ', 'd', 'e', 's', 'c', 'r', 'i', 'p', 't', 'i', 'o', 'n', ' ', '2']
['p', 'r', 'o', 'd', 'u', 'c', 't', ' ', 'd', 'e', 's', 'c', 'r', 'i', 'p', 't', 'i', 'o', 'n', ' ', '2']

这是actionchains源代码中的send_keys函数:

def send_keys(self, keys_to_send):
        """
        Sends keys to current focused element.

        :Args:
         - keys_to_send: The keys to send.  Modifier keys constants can be found in the
           'Keys' class.
        """
        typing = keys_to_typing(keys_to_send)
        if self._driver.w3c:
            for key in typing:
                self.key_down(key)
                self.key_up(key)
        else:
            self._actions.append(lambda: self._driver.execute(
                Command.SEND_KEYS_TO_ACTIVE_ELEMENT, {'value': typing}))
        return self

请帮我解释一下这种奇怪的情况吗? 我不知道为什么在循环中,actionchains.send_keys发送重复的内容? 谢谢!

1 个答案:

答案 0 :(得分:2)

问题是perform()不能正常工作(添加self.key_down(key)不能解决问题)。 self.key_up(key)中的send_keys()self.w3c_actions.key_action存储要在def key_down(self, value, element=None): if element: self.click(element) if self._driver.w3c: self.w3c_actions.key_action.key_down(value) self.w3c_actions.pointer_action.pause() else: self._actions.append(lambda: self._driver.execute( Command.SEND_KEYS_TO_ACTIVE_ELEMENT, {"value": keys_to_typing(value)})) return self 中键入的字符

reset_actions()

enter image description here

在调用def reset_actions(self): """ Clears actions that are already stored locally and on the remote end """ if self._driver.w3c: self.w3c_actions.clear_actions() self._actions = [] 时应清除这些动作

send_keys()

但是他们没有。

当使用产品描述2 调用key_action时,文本将添加到for,该文本已经包含键入产品描述1 的操作。从第一次ActionChains迭代开始,因此它会打印产品描述1产品描述2

可能的解决方案是在循环内创建for i in range(0, 2): try: body_text = list_test[i] driver.execute_script("window.scrollTo(0, document.body.scrollHeight)") item = WebDriverWait(driver, 10).until(ec.presence_of_element_located((By.TAG_NAME, "textarea"))) actions = ActionChains(driver) actions.move_to_element(item).send_keys(body_text).perform() time.sleep(1) except: pass 实例

{{1}}

更新

我在bugs.chromium开了一个问题。该问题已转载,但不会得到解决(在可预见的将来至少)。

相关问题