如何有效地将大字符串发送到 java selenium textarea 元素
How to efficiently send large String to java selenium textarea element
我需要将 String 类型的大文本发送到 textArea 元素。我用
driver.findElement(By.xpath(textarea_xpath)).sendKeys(fileText);
但是太慢了。有什么想法可以加快速度吗?
你可以用JS执行器试试:
public void enterTextJS(By locator, String text) {
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
WebElement element = driver.findElement(locator);
jsExecutor.executeScript("arguments[0].value='" + text + "';", element);
}
与Villa_7的解决方案基本相同。
设置 6k 个字符的文本大约需要 25 毫秒。
package selenium;
import java.time.LocalDateTime;
import java.util.Random;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class OmarHafez extends WebDriverSetup {
public static void main(String[] args) {
WebDriver driver = startChromeDriver();
driver.get("https://demoqa.com/automation-practice-form");
WebElement addressField = driver.findElement(By.tagName("textarea"));
addressField.click();
String s = randomStringOfLength(6000);
System.out.println(LocalDateTime.now());
String js = "document.evaluate(\"//textarea[@id='currentAddress']\", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.value = '" + s + "';";
JavascriptExecutor javascriptExecutor = (JavascriptExecutor)driver;
javascriptExecutor.executeScript(js);
System.out.println(LocalDateTime.now());
System.out.println(addressField.getAttribute("value"));
driver.quit();
}
public static String randomStringOfLength(int length) {
int leftLimit = 48; // numeral '0'
int rightLimit = 122; // letter 'z'
Random random = new Random();
String generatedString = random.ints(leftLimit, rightLimit + 1)
.filter(i -> (i <= 57 || i >= 65) && (i <= 90 || i >= 97))
.limit(length)
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
return generatedString;
}
}
public void enterTextJS(By locator, String text)
{
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
WebElement element = driver.findElement(locator);
jsExecutor.executeScript("arguments[1].value=arguments[0]", "'" + text + "'", element);}
这对我来说就像一个魅力。对于多行文本,这是最好的解决方案。谢谢@omar_Hafez
我需要将 String 类型的大文本发送到 textArea 元素。我用
driver.findElement(By.xpath(textarea_xpath)).sendKeys(fileText);
但是太慢了。有什么想法可以加快速度吗?
你可以用JS执行器试试:
public void enterTextJS(By locator, String text) {
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
WebElement element = driver.findElement(locator);
jsExecutor.executeScript("arguments[0].value='" + text + "';", element);
}
与Villa_7的解决方案基本相同。 设置 6k 个字符的文本大约需要 25 毫秒。
package selenium;
import java.time.LocalDateTime;
import java.util.Random;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class OmarHafez extends WebDriverSetup {
public static void main(String[] args) {
WebDriver driver = startChromeDriver();
driver.get("https://demoqa.com/automation-practice-form");
WebElement addressField = driver.findElement(By.tagName("textarea"));
addressField.click();
String s = randomStringOfLength(6000);
System.out.println(LocalDateTime.now());
String js = "document.evaluate(\"//textarea[@id='currentAddress']\", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.value = '" + s + "';";
JavascriptExecutor javascriptExecutor = (JavascriptExecutor)driver;
javascriptExecutor.executeScript(js);
System.out.println(LocalDateTime.now());
System.out.println(addressField.getAttribute("value"));
driver.quit();
}
public static String randomStringOfLength(int length) {
int leftLimit = 48; // numeral '0'
int rightLimit = 122; // letter 'z'
Random random = new Random();
String generatedString = random.ints(leftLimit, rightLimit + 1)
.filter(i -> (i <= 57 || i >= 65) && (i <= 90 || i >= 97))
.limit(length)
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
return generatedString;
}
}
public void enterTextJS(By locator, String text)
{
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
WebElement element = driver.findElement(locator);
jsExecutor.executeScript("arguments[1].value=arguments[0]", "'" + text + "'", element);}
这对我来说就像一个魅力。对于多行文本,这是最好的解决方案。谢谢@omar_Hafez