使用actionchains.send_keys时遇到错误(代码的最后一行)

我的代码:

from selenium import webdriver as wd
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time
import pyperclip

driver = wd.Chrome()

driver.get('https://temp-mail.org/en/') #opens the website
copy_btn = driver.find_element_by_xpath('//*[@id="click-to-copy"]') #Finds ID for copy button
copy_btn.click() #Clicks the copy button


driver.execute_script("window.open('');") #opens a new tab
time.sleep(3) #waits for tab to open
driver.switch_to.window(driver.window_handles[1]) #switches to the new tab

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') #Finds ID for search
ActionChains.send_keys(Keys.CONTROL + 'v').perform() #Pastes the text that was previously copied


错误:


Traceback (most recent call last):
  File "C:/Users/Shadow/PycharmProjects/untitled1/venv/Test.py", line 26, 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'



导致错误的代码:

ActionChains.send_keys(Keys.CONTROL + 'v').perform() #Pastes the text that was previously copied

最佳答案

通过直接发送来自元素的序列,可能会获得更好的结果:

input.send_keys(Keys.CONTROL, 'v').perform()

关于python - 在 Selenium 中使用ActionChains.send_keys时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57948218/

10-08 23:47