Performing a copy and paste with Selenium 2

后端 未结 8 1865
广开言路
广开言路 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:34

    I cannot try this on OSX at the moment, but it definitely works on FF and Ubuntu:

    import os
    import time
    
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    
    with open('test.html', 'w') as fp:
        fp.write("""\
    
    
      

    """) driver = webdriver.Firefox() driver.get('file:///{}/test.html'.format(os.getcwd())) element1 = driver.find_element_by_name('intext') element2 = driver.find_element_by_name('outtext') time.sleep(1) element1.send_keys(Keys.CONTROL, 'a') time.sleep(1) element1.send_keys(Keys.CONTROL, 'c') time.sleep(1) element2.send_keys(Keys.CONTROL, 'v')

    The sleep() statements are just there to be able to see the steps, they are of course not necessary for the program to function.

    The ActionChain send_key just switches to the selected element and does a send_keys on it.

提交回复
热议问题