gpt4 book ai didi

python - 在 python 中使用 selenium send_keys 复制文本

转载 作者:行者123 更新时间:2023-11-28 20:54:59 24 4
gpt4 key购买 nike

尝试使用 selenium python 命令复制文本,但由于某种原因它似乎不起作用

这是我的代码:

driver.get('https://temp-mail.org/en/') #opens the website
emailID = driver.find_element_by_xpath('//*[@id="mail"]') #find the email ID
ActionChains = ActionChains(driver)
ActionChains.double_click(emailID).perform()
ActionChains.send_keys(keys.CONTROL + 'c').perform()

代替:

ActionChains.send_keys(keys.CONTROL + 'c').perform()

我也试过:

emailID.send_keys(keys.CONTROL + 'c')

但似乎经常出现此错误:

module 'selenium.webdriver.common.keys' has no attribute 'CONTROL'

编辑:

driver.get('https://google.com ') #opens the website
input = driver.find_element_by_xpath('//*[@id="tsf"]/div[2]/div[1]/div[1]/div/div[2]/input')
ActionChains.send_keys(Keys.CONTROL + 'v').perform()

错误:

Traceback (most recent call last):
File "C:/Users/Shadow/PycharmProjects/untitled1/venv/Test.py", line 28, in <module>
ActionChains.send_keys(Keys.CONTROL + 'v').perform()
File "C:\Users\Shadow\PycharmProjects\untitled1\venv\lib\site-packages\selenium\webdriver\common\action_chains.py", line 336, in send_keys
if self._driver.w3c:
AttributeError: 'str' object has no attribute '_driver'

最佳答案

您的错误发生是因为您导入了模块 selenium.webdriver.common.keys

您应该在该模块中使用 Keys 类。

from selenium.webdriver.common.keys import Keys

#...

ActionChains.send_keys(Keys.CONTROL + 'c').perform()

编辑

其实就是将文本复制到剪贴板。您可以使用诸如 pyperclip 之类的库 获取文本。

from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import pyperclip
driver = Chrome('drivers/chromedriver')
driver.get('https://temp-mail.org/en/')
emailID = driver.find_element_by_xpath('//*[@id="mail"]')
ActionChains = ActionChains(driver)
ActionChains.double_click(emailID).perform()
ActionChains.send_keys(Keys.CONTROL + 'c').perform()
text = pyperclip.paste()
print(text)

输出

caberisoj@mail-file.net

关于python - 在 python 中使用 selenium send_keys 复制文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57947632/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com