目前,我需要复制订单 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")); ,
Run Code Online (Sandbox Code Playgroud)
然而,这无法复制任何内容,并且在粘贴时粘贴我自己之前复制的内容。
小智 6
您好,为什么您要处理特定的文本,即您的情况下的订单 id,为什么不使用 getText() 并将订单 id 保留在字符串中,然后将其传递到 sendKeys() 中,这将很简单且易于完成
String myOrderText = driver.findElement(By.xpath("ypur xpath to order id")).getText();
Run Code Online (Sandbox Code Playgroud)
并像下面一样使用它
driver.findElement (By.xpath("/html/body/main/div/div/div[2]/div/div/div[2]/div/table/tbody
/tr[2]/td[2]")).sendKeys(myOrderText ));
Run Code Online (Sandbox Code Playgroud)
另外,如果必须复制和粘贴,请按如下方式操作
使用 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"));
Run Code Online (Sandbox Code Playgroud)
希望这对您有帮助