我正在尝试在 Ubuntu 操作系统上使用 Python 3.8 将大量文本粘贴到带有 Selenium 的 input
元素。我可以遍历我的字符串和 send_keys
,但这需要 60 多秒。我希望我可以从我的 Python 代码中复制我的文本并使用 Ctrl + V 粘贴到 session 中:
我希望这行得通...
# How big do you want your character chunks?
n_chunk_size = 500
if len(keyphrase)>n_chunk_size:
pyperclip.copy(keyphrase)
input_element.send_keys(Keys.CONTROL, 'v')
慢速方式:from textwrap import wrap
for k in wrap(keyphrase, n_chunk_size):
input_element.send_keys(k)
最佳答案
所以,这个答案实际上是不同的。如果 textbox
您正在与之合作的是一家公司,可能是 input textbox
可能有 maxcharacter
限制。因此,您可能需要检查 length
您的string
在将其插入文本框之前。
对于这个例子,我使用了 W3Schools input textbox
并将我的环境变量插入到文本框中。在这个例子中,我导入了
import os as OperatingSystem
并且能够使用以下命令获取我的计算机的环境变量:# Get our operating system's environment information
environment_info = str(OperatingSystem.environ)
从那里,我能够创建一个 for-loop
检查了input text
我们的文本框,并将输入文本的长度与我们的 environment_info 的长度进行比较。主程序 - 供引用
from selenium import webdriver
from selenium.webdriver.chrome.webdriver import WebDriver as ChromeDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as DriverWait
from selenium.webdriver.support import expected_conditions as DriverConditions
from selenium.common.exceptions import WebDriverException
import os as OperatingSystem
import time
def get_chrome_driver():
"""This sets up our Chrome Driver and returns it as an object"""
path_to_chrome = "F:\Selenium_Drivers\Windows_Chrome85_Driver\chromedriver.exe"
chrome_options = webdriver.ChromeOptions()
# Browser is displayed in a custom window size
chrome_options.add_argument("window-size=1500,1000")
return webdriver.Chrome(executable_path = path_to_chrome,
options = chrome_options)
def wait_displayed(driver : ChromeDriver, xpath: str, int = 5):
try:
DriverWait(driver, int).until(
DriverConditions.presence_of_element_located(locator = (By.XPATH, xpath))
)
except:
raise WebDriverException(f'Timeout: Failed to find {xpath}')
# Get our operating system's environment information
environment_info = str(OperatingSystem.environ)
# Gets our Chrome driver and opens our site
chrome_driver = get_chrome_driver()
chrome_driver.get("https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_input_test")
wait_displayed(chrome_driver, "//iframe[@id='iframeResult']")
wait_displayed(chrome_driver, "//div[contains(@class, 'CodeMirror-wrap')]")
# Go into our iFrame
iFrame_Element = chrome_driver.find_element(By.XPATH, "//iframe[@id='iframeResult']")
chrome_driver.switch_to.frame(iFrame_Element)
wait_displayed(chrome_driver, "//form[contains(@action, 'action_page')]")
wait_displayed(chrome_driver, "//input[@id='fname']")
wait_displayed(chrome_driver, "//input[@id='lname']")
# Enter our information into our input textbox
chrome_driver.find_element(By.XPATH, "//input[@id='fname']").clear()
chrome_driver.find_element(By.XPATH, "//input[@id='fname']").send_keys(environment_info)
# Check that our textbox has been populated fully
for counter in range(5):
if chrome_driver.find_element(By.XPATH, "//input[@id='fname']").get_attribute('value').__len__() != environment_info.__len__():
if counter == 4:
raise Exception("Failed to input data into our textbox")
else:
time.sleep(3)
print(f'Counter is: {counter}')
else:
print('Input Textbox populated successfully')
break
chrome_driver.quit()
chrome_driver.service.stop()
关于python - 从环境变量复制文本并粘贴到 Selenium (Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64866501/