简体   繁体   English

在python中使用Selenium send_keys复制文本

[英]Copying text using selenium send_keys in python

Trying to copy text using selenium python commands but for some reason it doesn't seem to work 尝试使用Selenium python命令复制文本,但由于某种原因,它似乎不起作用

Here is my code: 这是我的代码:

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()

Instead of: 代替:

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

I have also tried: 我也尝试过:

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

But seem to constantly be getting this error: 但似乎总是不断出现此错误:

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

EDIT: 编辑:

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()

Error: 错误:

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'

why don't you just use text ? 你为什么不只使用text呢?

emailID = driver.find_element_by_xpath('//*[@id="mail"]')
text_emailID = emailID.text
print(text_emailID)

Update 更新

It seems to be hidden in the JS... so just use the Copy button! 它似乎隐藏在JS中...所以只需使用“ Copy按钮即可!

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

Your error is occurring b'coz you have imported the module selenium.webdriver.common.keys . 您导入了selenium.webdriver.common.keys模块时,发生了您的错误。

You should be using the Keys class within that module. 您应该在该模块中使用Keys类。

from selenium.webdriver.common.keys import Keys

#...

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

EDIT 编辑

It is actually copying the text to the clipboard. 它实际上是将文本复制到剪贴板。 You can use a library such as pyperclip to get the text. 您可以使用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)

Output 产量

caberisoj@mail-file.net

Never rely on clipboard in your automated tests, it is not safe. 切勿在自动化测试中依赖剪贴板,这是不安全的。 The tests must be fully atomic and independent and given you store the data in the clipboard it means that you will not be able to execute your Selenium tests in parallel using ie Selenium Grid 这些测试必须完全原子且独立,并且将数据存储在剪贴板中,这意味着您将无法使用Selenium Grid并行执行Selenium测试。

Also reconsider using your locator strategy , I would recommend locating elements by ID where possible as this is the fastest and the most reliable way. 还要重新考虑使用定位器策略 ,我建议尽可能通过ID定位元素,因为这是最快,最可靠的方法。

So if you run the following code: 因此,如果您运行以下代码:

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

you should see the temporary email address value in the terminal. 您应该在终端中看到临时电子邮件地址值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python selenium send_keys CONTROL,'c' 不复制实际文本 - python selenium send_keys CONTROL, 'c' not copying actual text 如何使用 selenium python 通过 send_keys 发送长文本 - how to send long text with send_keys using selenium python 在selenium python webdriver中使用send_keys()发送三个键 - To send threekeys using send_keys() in selenium python webdriver Python Selenium send_keys 函数发送部分文本 - Python Selenium send_keys function sends part of text Send_keys 长文本这么慢(Python + Selenium) - Send_keys long text so slowly (Python + Selenium) 有没有办法在 python selenium 中使用 send_keys 更改文本的颜色? - Is there any way to change the color of a text using send_keys in python selenium? 在Selenium Python中使用多个send_keys()无法正常工作 - Using multiple send_keys() in Selenium Python not working Selenium 未能使用 Python 在 Datepicker 字段上发送_keys - Selenium failed to send_keys on Datepicker field using Python 如何使用 selenium 和 Python 调用 send_keys() - How to invoke send_keys() using selenium and Python 在 Python (Selenium) 中使用 send_keys 时删除方括号 - Removing square brackets when using send_keys in Python (Selenium)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM