在python中使用selenium send_键复制文本

在python中使用selenium send_键复制文本,python,selenium,Python,Selenium,尝试使用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(em

尝试使用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'
回溯(最近一次呼叫最后一次):
文件“C:/Users/Shadow/PycharmProjects/untitled1/venv/Test.py”,第28行,in
ActionChains.send_key(key.CONTROL+'v').perform()
文件“C:\Users\Shadow\PycharmProjects\untitled1\venv\lib\site packages\selenium\webdriver\common\action\u chains.py”,第336行,在发送密钥中
如果是self.\u driver.w3c:
AttributeError:'str'对象没有属性'\u driver'

为什么不直接使用
文本呢

emailID = driver.find_element_by_xpath('//*[@id="mail"]')
text_emailID = emailID.text
print(text_emailID)
更新 它似乎隐藏在JS中。。。所以只需使用
复制
按钮

emailID = driver.find_element_by_xpath('//*[@id="mail"]')
emailID.click()
copy_btn = driver.find_element_by_xpath('//*[@id="click-to-copy"]')
copy_btn.click()

由于您已导入模块selenium.webdriver.common.keys
,因此发生了错误

您应该在该模块中使用

from selenium.webdriver.common.keys import Keys

#...

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

它实际上是将文本复制到剪贴板。您可以使用诸如的库来获取文本

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

在自动化测试中不要依赖剪贴板,它不安全。测试必须完全完成,如果您将数据存储在剪贴板中,则意味着您将无法

另外,请重新考虑使用,我建议尽可能使用,因为这是最快和最可靠的方法来定位元素

因此,如果运行以下代码:

driver.get("https://temp-mail.org/en/")
temp_email = driver.find_element_by_id("mail").get_attribute("value")
print(temp_email)

您应该可以在终端中看到临时电子邮件地址值。

请尝试从selenium.webdriver.common.keys导入keys
keys.CONTROL+'c'
不要忘记字母case应该是一个完整的工作示例。我相信它应该
keys.keys.CONTROL
。从selenium.webdriver.common.keys导入keys解决了这个问题。然而,即使代码运行并且没有错误,出于某种原因,它似乎没有复制文本。这似乎不起作用-我运行代码,没有错误,但它不会打印任何内容。我已删除selenium.webdriver.common.keys,而是从selenium.webdriver.common.keys导入密钥模块添加了这些密钥-但现在我遇到了以下错误:无法从selenium.webdriver.common导入名称“密钥”。keys@XxDanxX这是带大写字母的
钥匙
,非常感谢你!我不敢相信一件简单的事情会导致这样的错误,谢谢!我现在发现它似乎没有复制文本-我运行程序,让它复制,然后我停止程序并尝试粘贴它,但它没有粘贴文本谢谢。然而,我似乎得到了一个错误,上面写着“没有名为‘pyperclip’的模块”,尽管我已经导入了它——这是一个很好的答案@XxDanxX确保您没有任何名为pyperclip的文件夹。。。那会搞砸进口。。。(在我的回答中,我只是使用了按钮
Copy
…)正确!使用
get\u属性(“值”)
应该可以工作!