代码之家  ›  专栏  ›  技术社区  ›  Andrew

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

  •  1
  • Andrew  · 技术社区  · 8 年前

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

    <textfarea style=“position:absolute;padding:0px;width:1px;height:1em;outline:currentcolor none medium;”autocorrect=“off”autocapitalize=“off”spellcheck=“false”tabindex=“0”wrap=“off”></textfarea>
    < /代码> 
    
    

    这是我尝试使用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

    <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
    

    我也用过CSSL选择器访问元素,但仍有相同的错误。 如何正确访问弹出窗口?

    以下是更多的HTML代码:https://pastebin.com/6jdix2Cm

    2 回复  |  直到 8 年前
        1
  •  2
  •   cruisepandey    8 年前

    根据你的回答,这个 特克斯塔利亚 是在 伊夫拉姆 .

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

    要切换到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']"))  
    

    然后你可以与 特克斯塔利亚 AS:

    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.default_content()
    

    希望这会有所帮助。

        2
  •  0
  •   undetected Selenium    8 年前

    按照 HTML 你已经分享了,作为 <textarea> 在一个 <iframe> 所以你需要诱导 网络驱动程序 切换到所需的 框架 然后再次诱导 网络驱动程序 为所需 要点击的元素 在发送字符序列之前,可以使用以下解决方案:

    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