使用selenium和python在文本框中快速编写

2024-10-05 11:08:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Selenium和Python(Chorme驱动程序)在文本框中编写一些内容,但是有很多文本框,我需要它来更快地填充它们。我用一系列

driver.find_element_by_xpath("//input[@class='string required' and @id='order_billing_name']").send_keys("test.com")

但是写10-11这些命令需要很多时间。有没有办法加快速度?在


Tags: and内容inputstringbydriverselenium驱动程序
1条回答
网友
1楼 · 发布于 2024-10-05 11:08:43

您可以尝试使用javascript设置值:

orderBillingName = driver.find_element_by_id("order_billing_name")
driver.execute_script("arguments[0].value=arguments[1];", orderBillingName, "mytext")

您可以使用javascript直接设置:

^{pr2}$

许多字段的示例:

fields = {
    "input#order_billing_name": "some text", 
    ".order_number_class"     : "some text", 
    "css selector"            : "some text", 
    "css selector"            : "some text",
    "css selector"            : "some text"
    }

js = ""
for selector, value in fields.items():
    js = js + "document.querySelector('" + selector + "').value='"+ value +"';"

driver.execute_script(js)

相关问题 更多 >

    热门问题