如何使用selenium复制和粘贴值?

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

我目前需要复制订单 ID,然后将其粘贴到搜索字段中。

到目前为止我已经尝试过:

driver.findElement (By.xpath("/html/body/main/div/div/div[2]/div/div/div[2]/div/table/tbody/tr[2]/td[2]")).sendKeys(Keys.chord(Keys.CONTROL, "c")); ,

但是,这无法复制任何内容,并且在粘贴时粘贴我自己之前复制的内容。

Click here

java selenium
3个回答
6
投票

嗨,您为什么要处理特定的文本,即您的情况下的订单 id,为什么不使用 getText() 并将订单 id 保留在字符串中,然后将其传递到 sendKeys() 中,这将是简单且容易完成的

String myOrderText = driver.findElement(By.xpath("ypur xpath to order id")).getText();

并像下面这样使用它

driver.findElement (By.xpath("/html/body/main/div/div/div[2]/div/div/div[2]/div/table/tbody
/tr[2]/td[2]")).sendKeys(myOrderText ));

如果必须复制和粘贴,请按如下方式操作

使用selenium的actions类复制文本(订单id)

// or any locator strategy that you find suitable 
        WebElement locOfOrder = driver.findElement(By.id("id of the order id"));
Actions act = new Actions(driver);
act.moveToElement(locOfOrder).doubleClick().build().perform();
// catch here is double click on the text will by default select the text 
// now apply copy command 

driver.findElement(By.id("")).sendKeys(Keys.chord(Keys.CONTROL,"c"));
// now apply the command to paste
driver.findElement (By.xpath("/html/body/main/div/div/div[2]/div/div/div[2]/div/table/tbody/tr[2]/td[2]")).sendKeys(Keys.chord(Keys.CONTROL, "v"));

希望这对您有帮助


2
投票

您无需复印所有内容。您所要做的就是使用

getText()
。 尝试以下代码:

String mytext = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/div/div/div[2]/div/table/tbody/tr[2]/td[2]")).getText();
driver.findElement(By.xpath("your element path")).sendKeys(mytext);

谢谢你


0
投票
//IVS bob 
import java.io.FileInputStream;
import java.util.concurrent.TimeUnit;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

public class ivs_bob {
    WebDriver driver = null;

    @BeforeClass
    public void setup() {
        System.setProperty("webdriver.gecko.driver", "/home/user/Desktop/geckodriver");
        driver = new FirefoxDriver();
        driver.get("http://localhost:8080/application/");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

    }

    @DataProvider(name = "cred")
    public Object[][] getData() {
        Object[][] data = new Object[3][2];
        data[0][0] = "Selenium Using Python";
        data[0][1] = "Educator";

        data[1][0] = "Agile Testing";
        data[1][1] = "Co-Educator";
        return data;
    }

    @Test(priority = 1)
    public void add_mem() throws Exception {
        driver.findElement(By.xpath("/html/body/div/div/div[2]/p[2]/a[1]")).click();
        Thread.sleep(1000);
        FileInputStream fis = new FileInputStream(
                "/home/user/Desktop/user_repo/To_Participant/AdditionalSuppliedFiles/Items.xls");
        HSSFWorkbook wb = new HSSFWorkbook(fis);
        HSSFSheet sheet = wb.getSheet("Sheet1");
        for (int i = 1; i <= sheet.getLastRowNum(); i++) {
            HSSFRow row = sheet.getRow(i);
            String fname = row.getCell(0).getStringCellValue();
            String lname = row.getCell(1).getStringCellValue();
            String email = row.getCell(2).getStringCellValue();
            String empno = row.getCell(3).getStringCellValue();
            String stream = row.getCell(4).getStringCellValue();

            driver.findElement(By.xpath("/html/body/div[1]/form/input[1]")).sendKeys(fname);
            Thread.sleep(1000);

            driver.findElement(By.xpath("/html/body/div[1]/form/input[2]")).sendKeys(lname);
            Thread.sleep(1000);

            driver.findElement(By.xpath("//*[@id=\"email_form\"]")).sendKeys(email);
            Thread.sleep(1000);

            driver.findElement(By.xpath("//*[@id=\\\"password_form\\\"]]")).sendKeys(empno);
            Thread.sleep(1000);

            driver.findElement(By.xpath("")).sendKeys(stream);// incomplete xpath of stream
            Thread.sleep(1000);

        }
    }

    @Test(priority = 2)
    @Parameters({ "type", "pswd" })
    public void already_mem(String type, String pswd) throws Exception {
        driver.findElement(By.xpath("/html/body/div/div/div.div[2]/p[2]/a[2]")).click();
        Thread.sleep(1000);

        driver.findElement(By.id("inputName")).sendKeys(type);
        Thread.sleep(1000);

        driver.findElement(By.xpath("//*[@id=\"inputPassword\"]")).sendKeys(pswd);
        Thread.sleep(1000);

        driver.findElement(By.xpath("/html/body/div/form.button")).click();
        Thread.sleep(1000);

        String sgnout = driver.findElement(By.xpath("/html/body/nav/div/div[2]/ul/ul/li/a")).getText();

        SoftAssert asrt = new SoftAssert();
        asrt.assertEquals("SingOut", sgnout);
    }

    @Test(dataProvider = "cred", priority = 3)
    public void add_course(String course, String role) throws Exception {
        WebElement selectcourse = driver.findElement(By.id("course"));
        Select select1 = new Select(selectcourse);
        select1.selectByVisibleText(course);
        Thread.sleep(1000);

        WebElement selectrole = driver.findElement(By.xpath("//*[@id=\"role\"]"));
        Select select2 = new Select(selectrole);
        select2.selectByVisibleText(role);
        Thread.sleep(1000);

        driver.findElement(By.xpath("/html/body/div[3]/form/button[1]")).click();
        Thread.sleep(1000);

        driver.findElement(By.xpath("/html.body/nav/div/div[2]/ul/li/a")).click();
        Thread.sleep(1000);
    }

    @AfterClass
    public void teardown() {
        driver.quit();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.