如何使用selenium复制和粘贴值?
我目前需要复制订单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")); ,
然而,这无法复制任何东西,粘贴时粘贴我之前自己复制的内容。
点击这里
嗨,为什么你要处理一个特定的文本,即你的情况下的订单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的动作类来复制文本(订单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"));
希望这对你有所帮助
您不需要复制所有内容。 您所要做的就是使用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);
谢谢