How to send "\t" or "tab" within a string into textbox in selenium without using Clipboard or Actions CTRL+C, CTRL+V

huangapple go评论63阅读模式
英文:

How to send "\t" or "tab" within a string into textbox in selenium without using Clipboard or Actions CTRL+C, CTRL+V

问题

以下是您要翻译的内容:

我有一个类似这样的字符串。

String inputString = "This\tis\ta\tString";
Webelement inputbox = inputBoxXpath;
inputbox.sendKeys(inputString);

上述代码的问题在于,它首先发送了 'This',然后 \t 被视为按下 TAB 键,从而将焦点移动到下一个元素。

为了避免这种情况,我正在使用剪贴板的方法,只要应用程序处于焦点状态,这个方法就可以完美地工作。

//部分解决方案
StringSelection selection = new StringSelection(inputString);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(selection, selection);
inputbox.click()
send_keys("CTRL+V") // 将剪贴板中的字符串粘贴出来。

这种方法有效,然而,当程序正在运行时,如果我将其他内容复制到剪贴板中,那么inputString将被剪贴板的内容替换掉。

这也会在我想在服务器虚拟机上运行代码时引发问题。

我还尝试过使用 Actions 来发送按键,然而输出仍然存在问题。

我正在寻找的是一种使用本地的 sendKeys('string') 方法的方式,其中 string 包含 \t 和 \n,而无需使用 CTRL+C 和 CTRL+V,也无需使用 getCLipboard 和 setClipboard。

英文:

I have a string like this.

String inputString="This\tis\ta\tString";
Webelement inputbox=inputBoxXpath;
inputbox.sendKeys(inputString);

The problem with code above is that it sends 'This' then \t is considered a keypress TAB which then moves the focus to next element.

To avoid that I am using the Clipboard way and that works flawlessly as long as the app is in focus.

//Partial solution
StringSelection selection = new StringSelection(inputString);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(selection, selection);
inputbox.click()
send_keys("CTRL+V") // Paste the string in clipboard.

This method works however, when the program is running and I copy something else to Clipboard then the inputString gets replaced by the contents of Clipboard.

This also causes issues if I want to run the code on a server VM

I have also tried Actions to send the keys however the output is still faulty.

What I am looking for is a way to use native sendKeys('string') where string contains \t and \n, without using CTRL+C and CTRL+V or getCLipboard and setClipboard.

答案1

得分: 1

你可以使用 JavaScript 来完成这个操作。这样可以直接将值设置到对象中。

在 Java 中,你可以像这样执行:

    @Test
    public void OptionClickTest() throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
        var driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://www.google.com");

        var object = driver.findElement(By.xpath("//input[@name='q']"));
        ((JavascriptExecutor) driver).executeScript("arguments[0].value='this\\tis\\tmy\\ttext'", object);
}

这不适用于所有类型的输入框,但在谷歌上对我有效,值得一试:

How to send "\t" or "tab" within a string into textbox in selenium without using Clipboard or Actions CTRL+C, CTRL+V

英文:

You can do it with javascript. This allow you to set the value directly into the object.

In Java you execute it like this:

    @Test
    public void OptionClickTest() throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
        var driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://www.google.com");

        var object = driver.findElement(By.xpath("//input[@name='q']"));
        ((JavascriptExecutor) driver).executeScript("arguments[0].value='this\tis\tmy\ttext'", object);

}

It doesn't work on all types of input boxes - but it worked for me on google so it's worth a go:

How to send "\t" or "tab" within a string into textbox in selenium without using Clipboard or Actions CTRL+C, CTRL+V

答案2

得分: 0

将您的输入字符串中的'\t'替换为'\u0009'Java中Unicode字符'CHARACTER TABULATION'的编码)。

英文:

Replace '\t' in your input String with '\u0009' (Java Encoding for Unicode Character 'CHARACTER TABULATION')

huangapple
  • 本文由 发表于 2020年8月25日 20:03:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63578451.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定