如何将文字输入' textarea'标签使用硒?

时间:2016-12-14 10:37:18

标签: selenium textarea

我想输入文字" ABCD"如下图所示 https://translate.google.com/

尝试了以下方法,但失败了。

  1. 的SendKeys() - 失败

    driver.findElement(By.xpath(" // textarea的[@id =' sourceis']&#34))的SendKeys。(" ABCD&#34);  driver.findElement(By.xpath(" // textarea的[@id ='源极 - 是']&#34))。的SendKeys(Keys.TAB" ABCD&#34)

  2. 使用JavaScriptExecutor设置value属性 - 失败

    JavascriptExecutor js =(JavascriptExecutor)驱动程序;         js.executeScript(" document.getElementById(' source-is')。setAttribute(' value',' ABCD')");

  3. 首先是clear()字段,然后是sendKeys() - 失败。

    driver.findElement(By.xpath(" // textarea的[@id ='源极 - 是']&#34))明确();     driver.findElement(By.xpath(" // textarea的[@id ='源极 - 是']&#34))的SendKeys(Keys.TAB," ABCD&#34 );

  4. 请提供解决方案以完成任务。

    textarea field

4 个答案:

答案 0 :(得分:1)

我不知道您是怎么做的,但以下代码有效:

driver.findElement(By.id("source")).sendKeys("your text to enter");

答案 1 :(得分:0)

使用此 -

driver.findElement(By.id("source")).sendKeys("ABCD");

答案 2 :(得分:0)

在Selenium IDE Firefox(v68)扩展中对我有用的是

  • 命令:execute script
  • 目标:document.getElementById('message').value = "Testmessage at " + new Date();
  • 值:

答案 3 :(得分:0)

selenium ide ++中,带有type的简单宏对我有用:

{
  "Name": test",
  "CreationDate": "2019-7-9",
  "Commands": [
    {
      "Command": "open",
      "Target": "https://translate.google.com/",
      "Value": ""
    },
    {
      "Command": "type",
      "Target": "id=source",
      "Value": "hello world"
    }
  ]
}

您可以将此代码粘贴到JSON code tab.