我正在尝试复制并过去简单的文字,下面不需要的输出是我的代码
public class practiceExample2 {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "G:\\TopsAssignment\\SampleJavaExample\\lib\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.facebook.com/");
WebElement elm1 = driver.findElement(By.id("email"));
WebElement elm2 = driver.findElement(By.id("pass"));
Actions cpypast = new Actions(driver);
Action write = cpypast.sendKeys(elm1, "Pratik").build();
Action select = cpypast.doubleClick(elm1).build();
Action copy = cpypast.sendKeys(elm1, Keys.CONTROL).sendKeys(elm1, "C").build();
Action past = cpypast.sendKeys(elm2,Keys.CONTROL).sendKeys(elm2,"V").build();
write.perform();
select.perform();
copy.perform();
past.perform();
String str = elm2.getText();
System.out.println(str);
driver.close();
}
}
答案 0 :(得分:0)
如果您能够成功选择文本值,则问题仅在于“复制”和“复制”。和'粘贴',您可能会发现此解决方案很有用 -
问题专栏:
Action copy = cpypast.sendKeys(elm1, Keys.CONTROL).sendKeys(elm1, "C").build();
而是使用此代码:
Action copy = cpypast.sendKeys(Keys.CONTROL).sendKeys("C").build();
答案 1 :(得分:0)
我尝试使用Robot类很好用
public class practiceExample2 {
public static void main(String[] args) throws AWTException {
System.setProperty("webdriver.chrome.driver", "G:\\TopsAssignment\\SampleJavaExample\\lib\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.facebook.com/");
WebElement elm1 = driver.findElement(By.name("firstname"));
String test = elm1.getText();
System.out.println(test);
WebElement elm2 = driver.findElement(By.name("lastname"));
Actions cpypast = new Actions(driver);
cpypast.sendKeys(elm1,"Pratik").doubleClick().contextClick().build().perform();
Robot rb = new Robot();
rb.keyPress(KeyEvent.VK_DOWN);
rb.keyRelease(KeyEvent.VK_DOWN);
rb.keyPress(KeyEvent.VK_DOWN);
rb.keyPress(KeyEvent.VK_ENTER);
rb.keyRelease(KeyEvent.VK_DOWN);
rb.keyRelease(KeyEvent.VK_ENTER);
Actions cpypast1 = new Actions(driver);
cpypast1.contextClick(elm2).build().perform();
rb.keyPress(KeyEvent.VK_DOWN);
rb.keyRelease(KeyEvent.VK_DOWN);
rb.keyPress(KeyEvent.VK_DOWN);
rb.keyPress(KeyEvent.VK_ENTER);
rb.keyRelease(KeyEvent.VK_DOWN);
rb.keyRelease(KeyEvent.VK_ENTER);
String str = elm2.getText();
System.out.println(str);
driver.close();
}
}