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

如何根据HTML通过Selenium和Python将文本发送到textarea,python,selenium,selenium-webdriver,xpath,webdriverwait,Python,Selenium,Selenium Webdriver,Xpath,Webdriverwait,无法确定如何将文本写入弹出窗口,以下是弹出窗口的外观: 获取页面上找不到元素的错误信息: selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: /html/body/div/div[1]/textarea 我也使用了css\u选择器来访问元素,但仍然存在相同的错误。 如何正确访问弹出窗口 下面是更多的HTML代码:根据您的回答,这个文本区域位于iframe中 首先,您必须切换到

无法确定如何将文本写入弹出窗口,以下是弹出窗口的外观:

获取页面上找不到元素的错误信息:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: /html/body/div/div[1]/textarea
我也使用了css\u选择器来访问元素,但仍然存在相同的错误。 如何正确访问弹出窗口


下面是更多的HTML代码:

根据您的回答,这个文本区域位于iframe

首先,您必须切换到框架,然后才能与此文本区域进行交互

要切换到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']"))  
driver.switch_to.default_content()
然后,您可以通过以下方式与文本区域进行交互:

driver.find_element_by_css_selector("textarea[spellcheck='false'][wrap='off'][style$='outline: currentcolor none medium;']").send_keys("Some text here")  
完成特定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']"))  
driver.switch_to.default_content()

希望这会有所帮助。

根据您的回答,此文本区域位于iframe

首先,您必须切换到框架,然后才能与此文本区域进行交互

要切换到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']"))  
driver.switch_to.default_content()
然后,您可以通过以下方式与文本区域进行交互:

driver.find_element_by_css_selector("textarea[spellcheck='false'][wrap='off'][style$='outline: currentcolor none medium;']").send_keys("Some text here")  
完成特定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']"))  
driver.switch_to.default_content()
希望这会有所帮助。

根据您共享的HTML,因为
中,所以您需要诱导WebDriverWait切换到所需的帧,然后再次诱导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
根据您共享的HTML,由于
中,因此您需要诱导WebDriverWait切换到所需的帧,然后再次诱导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

好像在iframe中。你能在这个
textarea
上多分享一些HTML吗这里有更多的代码。Textarea被包装在iframe中@Andrew:你能给我们更新吗?答案中有一个有用吗?还是你还面临什么问题?@cruisepandey非常感谢它解决了我的问题!好像在iframe中。你能在这个
textarea
上多分享一些HTML吗这里有更多的代码。Textarea被包装在iframe中@Andrew:你能给我们更新吗?答案中有一个有用吗?还是你还面临什么问题?@cruisepandey非常感谢它解决了我的问题!driver.switch\u to.frame()已被弃用,driver.switch\u to.defaultContent()不是WebDriver的python实现API@JanRozycki:你在谈论硒的哪个版本?我一直在使用selenium 3.12.0,它工作得很好。除此之外,从python使用驼峰大小写和分号开始?;)@JanRozycki,
switch_to.frame()
未被弃用。不推荐使用的是
switch_to_frame()
@cruisepandey使用
switch_to.default_content()
而不是
switch_to.defaultContent()
driver.switch_to.frame()已被弃用,driver.switch_to.defaultContent()不是WebDriver的python实现API@JanRozycki:你在谈论硒的哪个版本?我一直在使用selenium 3.12.0,它工作得很好。除此之外,从python使用驼峰大小写和分号开始?;)@JanRozycki,
switch_to.frame()
未被弃用。不推荐使用的是
switch_to_frame()
@cruisepandey使用
switch_to.default_content()
而不是
switch_to.defaultContent()