使用 selenium 中的 send_keys 发送超过 200000 个字符

发布于 2025-01-10 09:44:46 字数 768 浏览 0 评论 0原文

我正在尝试从 Excel 工作表创建 HTML 表并将其复制到网页。我正在使用 Send_Keys 向网页发送超过 200000 个字符(无关行),但它导致内存问题并使 jupyter 崩溃。我的代码如下。我正在寻找方法/替代方案来加快在网页上的代码中复制变量 x 的过程。

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 30)

sheet_to_df_map = pd.read_excel(r'.xlsx', sheet_name='')

x = sheet_to_df_map.to_html()
x = str(x)

wait.until(EC.element_to_be_clickable((By.ID, "editPageLink"))).click()

wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID,"wysiwygTextarea_ifr")))

button1 = wait.until(EC.element_to_be_clickable((By.XPATH, "//body[@data-id='wysiwygTextarea']//p")))

button1.click()
button1.send_keys(x)

I am trying to create an HTML table from excel sheet and copy it to a webpage. I am using Send_Keys to send over 200000 characters (indifferent lines) to a webpage but it is causing memory issue and crashing jupyter. My code is as below. I am looking for ways/alternatives to speed up the process to copy the variable x in my code on the webpage.

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 30)

sheet_to_df_map = pd.read_excel(r'.xlsx', sheet_name='')

x = sheet_to_df_map.to_html()
x = str(x)

wait.until(EC.element_to_be_clickable((By.ID, "editPageLink"))).click()

wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID,"wysiwygTextarea_ifr")))

button1 = wait.until(EC.element_to_be_clickable((By.XPATH, "//body[@data-id='wysiwygTextarea']//p")))

button1.click()
button1.send_keys(x)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫南 2025-01-17 09:44:46

当我以前遇到这个问题时,我把字符串分成了几批。你可以按换行符之类的东西进行分割,但如果你不知道每行有多长,你可以简单地按特定长度(字符数)进行分割,但如果你有很多 unicode 和东西,这可能不是最好的方法之一。判断什么最适合您。

interval = 1000 # No of characters
lines = [x[i:i+interval] for i in range(0, len(x), interval)]

for l in lines:
    button1.send_keys(l)

个人评论:但这不是一种节省时间的方法,因为它大大增加了输入所有内容所需的时间。我最终在做其他事情的同时使用多线程来完成这项工作。我确信其他人可能能够提出更好的解决方案,但这就是我所选择的。

When I faced this problem before, I broke my string up into batches. You can split by something like newline, but if you don't know how long each line will be, you can simply split by specific lengths (number of characters), but if you have a lot of unicode and stuff, this may not be the best approach either. Judge what works best for you.

interval = 1000 # No of characters
lines = [x[i:i+interval] for i in range(0, len(x), interval)]

for l in lines:
    button1.send_keys(l)

Personal Comment: This is not a time efficient approach however because it significantly increases time taken to type in everything. I ended up using multi-threading to make this work while I did other things. I'm sure others may be able to come up with better solutions, but this is what I settled for.

下雨或天晴 2025-01-17 09:44:46

对于我的用例,我必须发送 > 400k 个字符。将绳子分成几块对我来说不起作用。我能够使用 JavaScript:

@FindBy(name = "...")
private WebElement messageTxt;

public void setMessage(StringBuilder message) {
    messageTxt.clear();
    JavascriptExecutor jsDriver = (JavascriptExecutor) driver;
    jsDriver.executeScript("arguments[0].value=arguments[1];", messageTxt, message.toString());
}

上面是一个 Java 解决方案,但这个概念在任何语言绑定中都应该是相同的。

For my usecase I had to send >400k characters. Chunking up the string into pieces did not work for me. I was able to use JavaScript:

@FindBy(name = "...")
private WebElement messageTxt;

public void setMessage(StringBuilder message) {
    messageTxt.clear();
    JavascriptExecutor jsDriver = (JavascriptExecutor) driver;
    jsDriver.executeScript("arguments[0].value=arguments[1];", messageTxt, message.toString());
}

The above is a Java solution, but the concept should be the same in any language binding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文