无法点击文本区域

Unable to click textarea

我正在做一个用 Java 编码的项目,我正在使用 Selenium 来自动化一个过程。
我有这两个文本区域,将我的文本发送到第一个区域完全没有问题,因为我可以通过 id.

找到它
<td class="tdLeft">:</td>
<td class="tdRight">
<textarea placeholder="" id="add_links" name="links[]"></textarea>
</td>

driver.findElementById("add_links").sendKeys("\n");

但是第二个文本区域我遇到了问题。

<tr>
<td class="tdLeft">:</td>
<td class="tdRight">
<textarea name="links[]"></textarea>
</td>
</tr>

我怎样才能把我的文字放到这个里面?

如果你知道 textarea 索引,你可以试试下面的方法,

driver.findElementsByXPath("//textarea").get(1).sendKeys("\n");

您有 2 个具有相同 name 属性值的文本区域。您可以为第二个文本区域执行以下操作:

List<WebElement> linksize=null;
 String arraylinks[]=null;
linksize = driver.findElements(By.name("links[]")); 
    int linksCount = linksize.size();
    arraylinks= new String[linksCount];
    for(int i=0;i<linksCount;i++)
    {

    arraylinks[i] = linksize.get(i).getAttribute("id");
    if(arraylinks[i].isEmpty())
    {

        System.out.println("I am second text area"+arraylinks[i]);
        linksize.get(i).sendKeys("Hello");


    }

    }

这里我们得到 text area 具有共同属性,遍历它们并仅将文本发送到没有 id 的属性。它对我有用。试一试。

试试这个 xpath,driver.findElement(By.xpath("//tr/td[2]/textarea[@]"))

您可以尝试使用 cssSelectors:

driver.findElement(By.cssSelector("textarea[name='links[]']"));

很长的路要走:

List<WebElement> txtAreas = driver.findElements(By.xpath("//textarea[name='links[]']"));
for (WebElement txtArea : txtAreas) { 
    if (!txtArea.getAttribute("id").equalsIgnoreCase("add_links")) {
        txtArea.sendKeys("\n");
        break;
    }
}

最简单的方法是:

WebElement txtArea = driver.findElement(By.xpath("//textarea[name='links[]' and not(@id = 'add_links')]"));
txtArea.sendKeys("\n");

我发现双击然后发送密钥作为解决方案:D

Actions action = new Actions(driver);
action.moveToElement(findElement(locator)).doubleClick().build().perform();
locator.sendKeys("Text");