首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用selenium和python在textBox中快速编写

使用selenium和python在textBox中快速编写
EN

Stack Overflow用户
提问于 2018-09-14 16:46:45
回答 1查看 662关注 0票数 0

我使用Selenium和Python (Chorme驱动程序)在文本框中写东西,但是有很多文本框,我需要它来更快地填充它们。我使用的序列

代码语言:javascript
复制
driver.find_element_by_xpath("//input[@class='string required' and @id='order_billing_name']").send_keys("test.com")

命令,但是要编写10-11命令需要很长时间。有办法加快速度吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-14 18:45:49

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

代码语言:javascript
复制
orderBillingName = driver.find_element_by_id("order_billing_name")
driver.execute_script("arguments[0].value=arguments[1];", orderBillingName, "mytext")

您可以使用javascript直接设置所有内容:

代码语言:javascript
复制
driver.execute_script("document.querySelector('#order_billing_name').value='mytext';"\
                   "document.querySelector('.order_number_class').value='12323';"\
                   "document.querySelector('input#anotherinputid').value='anything';")

许多字段的示例:

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52336190

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档