如何根据 HTML 通过 Selenium 和 Python 将文本发送到文本区域

How do I send text into a textarea through Selenium and Python as per the HTML

不知道如何将文本写入弹出窗口,弹出窗口如下所示:

<textarea style="position: absolute; padding: 0px; width: 1px; height: 1em; outline: currentcolor none medium;" autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" wrap="off"></textarea>

这是我尝试使用 XPath 访问它的方式:

driver.find_element_by_xpath("/html/body/div/div[1]/textarea").send_keys("Some text here")

获取页面上未找到元素的错误:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: /html/body/div/div[1]/textarea

我也用了css_selector访问元素,还是一样的错误。 如何正确访问弹出窗口?

这里有更多HTML代码:https://pastebin.com/6jdix2Cm

根据您的回复,此 textareaiframe 中。

首先你必须切换到框架,然后你才能与这个文本区域进行交互。

要切换到 iframe,您可以使用此代码:

driver.switch_to.frame(driver.find_element_by_xpath("//iframe[@src='https://qsm.qoo10.sg/gmkt.inc.gsm.web/common/scripts/module/tiny_mce_4.5.7/source/plugins/codemirror/source.html']"))  

然后您可以与 textarea 互动:

driver.find_element_by_css_selector("textarea[spellcheck='false'][wrap='off'][style$='outline: currentcolor none medium;']").send_keys("Some text here")  

切换到默认内容总是好的,一旦您完成了特定的 iframe.For,您将需要此代码:

driver.switch_to.default_content()

希望这会有所帮助。

根据您分享的 HTML,因为 <textarea><iframe> 中,所以您需要引入 WebDriverWait 切换到所需的 frame 然后再次为所需的 元素引入 WebDriverWait在发送字符序列之前可以点击,可以使用以下解决方案:

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[contains(@src,'gmkt.inc.gsm.web/common/scripts/module/tiny_mce_4.5.7/source/plugins/codemirror/source.html')]")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.TAG_NAME, "textarea"))).send_keys("Andrew")

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

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