1

我使用 python + selenium 将一些文本添加到文本区域(在 DOM 中它是一个 DIV 标记)。

例如,我有以下文本:

EVERY SINGLE picture or video you will publish with

使用send_keys()函数后,textarea 中的文本如下:

EVERY SINGLE ice o ideo o ill blih ih

代码片段:

message = 'EVERY SINGLE picture or video you will publish with'
reply_input = WebDriverWait(driver, 10).until(
                                    EC.presence_of_element_located((By.CSS_SELECTOR, 'div[aria-label="Message Body"]'))
                                )
reply_input.clear()
reply_input.click()
reply_input.send_keys(message)

问题是,该问题无法稳定地重现,它时常出现。

有谁知道如何解决这样的问题?

4

1 回答 1

-1

presence_of_element_located()不能确保元素是可交互的。相反,您需要诱导WebDriverWait并且element_to_be_clickable()您可以使用以下Locator Strategy

message = 'EVERY SINGLE picture or video you will publish with'
reply_input = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[aria-label="Message Body"]")))
reply_input.click()
reply_input.clear()
reply_input.send_keys(message)

注意:您必须添加以下导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

参考

您可以在以下位置找到一些相关的详细讨论:

于 2021-01-18T19:16:24.737 回答