[英]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)
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.