gpt4 book ai didi

python - 集成到 for 循环 python 时由 actionchains send_keys 发送的重复文本

转载 作者:太空宇宙 更新时间:2023-11-04 01:55:56 26 4
gpt4 key购买 nike

我尝试将 send_keys 发送到 textarea,所以我使用 actionchains 发送 key 。我使用了这段代码:

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

文本结果被发送到谷歌翻译如下:

product description 1product description 1product description 2

你可以看到这很奇怪,它应该是这样的:

product description 1product description 2

我还在 actionchains 的源代码中将 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

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

最佳答案

问题是 reset_actions() 没有按预期工作(添加 perform() 没有解决问题)。 self.key_down(key)self.key_up(key) in 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

enter image description here

调用 reset_actions() 时应清除这些操作

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()product description 2 调用时,文本被添加到 key_action,它已经包含了输入 的操作>product description 1 来自第一个for 迭代,因此它打印product description 1product description 2

可能的解决方案是在循环内创建ActionChains实例

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

更新

我在 bugs.chromium 中打开了一个问题.该问题已重现,但不会得到修复(至少在可预见的 future )。

关于python - 集成到 for 循环 python 时由 actionchains send_keys 发送的重复文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56822201/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com