我想双击整个文本区域中的一个单词。但每当我尝试查找元素并双击时,它都会选择整个文本区域

问题描述 投票:0回答:1

这是我的文本区域,我想单击任何一个单词,假设我想单击“星期六”,但是这个 xpath 给出了整个文本区域,无法单击任何一个单词。请让我知道如何使用 Java selenium 来做到这一点

我尝试了xpath和doubleClick方法

java selenium-webdriver cucumber bdd
1个回答
0
投票

//you need to use js, firstly focus the entire text, then search the word //you need then select it and click on it using js executar   

 WebElement textArea = driver.findElement(By.id("ai-script.....ETC"));

 
        String theWordYouNeedToClick = "Saturday";
        String script = "var textArea = arguments[0]; " +
                        "textArea.focus(); " +
                        "var start = textArea.value.indexOf('" + theWordYouNeedToClick + "'); " +
                        "var end = start + '" + theWordYouNeedToClick + "'.length; " +
                        "textArea.setSelectionRange(start, end); " 
        ((JavascriptExecutor) driver).executeScript(script, textArea);

你需要使用js,首先聚焦整个文本,然后搜索你需要的单词然后选择它并使用js执行器点击它

WebElement textArea = driver.findElement(By.id("ai-script.....ETC"));

    String theWordYouNeedToClick = "Saturday";
    String script = "var textArea = arguments[0]; " +
                    "textArea.focus(); " +
                    "var start = textArea.value.indexOf('" + theWordYouNeedToClick + "'); " +
                    "var end = start + '" + theWordYouNeedToClick + "'.length; " +
                    "textArea.setSelectionRange(start, end); " 
    ((JavascriptExecutor) driver).executeScript(script, textArea);
© www.soinside.com 2019 - 2024. All rights reserved.