Selenium webdriver:双击并复制粘贴不起作用

时间:2017-10-03 05:56:43

标签: java selenium webdriver action

我正在尝试在yahoo.com上双击并复制粘贴方案,但它无法正常工作。想要复制文本"登录"并粘贴用户名

{    package com.yahoo.com;

    import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.By;`
    import org.openqa.selenium.Keys;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.interactions.Actions;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.Test;

    public class YahooTests {
     WebDriver driver;

    @Test
    public void test01_InvokeBrowserApp(){
    System.setProperty("webdriver.chrome.driver", "C:\\SeleniumDrivers\\chromedriver_win32\\chromedriver.exe");
    driver = new ChromeDriver();
  driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    driver.manage().window().maximize();

    }

     {   Actions act = new Actions(driver);

    WebElement copy = driver.findElement(By.xpath(".//*[@id='mbr-login-greeting']"));



  act.moveToElement(copy).doubleClick().sendKeys(Keys.CONTROL+"C").build().perform();
       act.doubleClick(paste).sendKeys(Keys.CONTROL+"V");
WebElement paste = driver.findElement(By.xpath(".//*[@id='login-username']"));

}

1 个答案:

答案 0 :(得分:0)

尝试这个希望问题将得到解决

act.moveToElement(By.xpath(".//*[@id='mbr-login-greeting']")).doubleClick().build().perform();
    // catch here is double click on the text will by default select the text 
    // now apply copy command 

    driver.findElement(By.xpath(".//*[@id='mbr-login-greeting']")).sendKeys(Keys.chord(Keys.CONTROL,"c"));
    // now apply the command to paste
    driver.findElement (By.xpath(".//*[@id='login-username']").sendKeys(Keys.chord(Keys.CONTROL, "v"));