gpt4 book ai didi

python - 在 selenium 中使用 ActionChains.send_keys 时出错

转载 作者:太空宇宙 更新时间:2023-11-03 20:24:13 25 4
gpt4 key购买 nike

我在使用 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/

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