Performing a copy and paste with Selenium 2

后端 未结 8 1887
广开言路
广开言路 2020-11-30 05:05

Is there any way to perform a copy and paste using Selenium 2 and the Python bindings?

I\'ve highlighted the element I want to copy and then I perform the following

8条回答
  •  忘掉有多难
    2020-11-30 05:28

    Rather than using the actual keyboard shortcut i would make the webdriver get the text. You can do this by finding the inner text of the element.

    WebElement element1 = wd.findElement(By.locatorType(locator));
    String text = element1.getText();
    

    This way your test project can actually access the text. This is beneficial for logging purposes, or maybe just to make sure the text says what you want it to say.

    from here you can manipulate the element's text as one string so you have full control of what you enter into the element that you're pasting into. Now just

     element2.clear();
     element2.sendKeys(text);
    

    where element2 is the element to paste the text into

提交回复
热议问题