首页 > 解决方案 > 如何从python中的文本文件中复制所有文本(使用ctrl + c和ctrl + a命令)?

问题描述

我正在写一个硒项目。

我试图创建一个函数,使用 ctrl + A、ctrl + C 复制文本文件中的所有文本。

def Copy(driver) :
    f = open("out1.txt") # open the text file

        .click() #i need to click on the file
        .send_keys(Keys.CONTROL, 'a') # select all the text
    time.sleep(1)
        .send_keys(Keys.CONTROL, 'c') # copy the text in my clipboard

    f.close()

这就是我尝试编写的代码,但我没有管理。有什么帮助吗?

标签: pythonpython-3.xseleniumfileselenium-chromedriver

解决方案


您应该使用 Python 的内置文件读/写函数。 https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files

对于您的示例,您可以这样做。

with open('out1.txt', 'r') as f:
    data = f.readlines()

推荐阅读