我使用Selenium和Python (Chorme驱动程序)在文本框中写东西,但是有很多文本框,我需要它来更快地填充它们。我使用的序列
driver.find_element_by_xpath("//input[@class='string required' and @id='order_billing_name']").send_keys("test.com")
命令,但是要编写10-11命令需要很长时间。有办法加快速度吗?
发布于 2018-09-14 18:45:49
您可以尝试使用javascript设置值:
orderBillingName = driver.find_element_by_id("order_billing_name")
driver.execute_script("arguments[0].value=arguments[1];", orderBillingName, "mytext")您可以使用javascript直接设置所有内容:
driver.execute_script("document.querySelector('#order_billing_name').value='mytext';"\
"document.querySelector('.order_number_class').value='12323';"\
"document.querySelector('input#anotherinputid').value='anything';")许多字段的示例:
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)https://stackoverflow.com/questions/52336190
复制相似问题