使用Selenium和python在textBox中快速编写 - javascript

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

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

命令,但要编写10-11这些命令会花费很多时间。有办法加快速度吗?

参考方案

您可以尝试使用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)

如何在没有for循环的情况下在Javascript中使用Django模板标签 - javascript

我想在JavaScript中使用模板变量:我的问题是在javascript代码中使用for循环,for循环之间的所有事情都会重复..但我不想要....下面粘贴了我的代码..有人可以告诉我更好的方法吗这..因为这看起来很丑..这是我的代码: {% block extra_javascript %} <script src="/static/js…

使用JS和PHP更改弹出窗口背景图像 - javascript

我有一个JS函数:function zoom(now) { document.getElementById("popup").style.display = "block"; document.getElementById("photos").style.backgroundImage = …

打印二维阵列 - javascript

我正在尝试打印子元素。在this example之后。怎么做?。$myarray = array("DO"=>array('IDEAS','BRANDS','CREATIVE','CAMPAIGNS'), "JOCKEY"=>a…

执行onclick时获得意外令牌 - javascript

我正在使用onclick事件从PHP调用JS函数。这是我的代码:我在一个函数中,因此我需要通过PHP来完成它,因为然后我会返回:$html = '<input type="checkbox" checked value="1" id="setGetSku" name="se…

Javascript到PHP的转换 - javascript

我有一个libphonenumber软件包的javascript端口,它具有以下功能:function cleanPhone(a){ a=a.replace(/[^\d\+]/g,""); return a="+"==a.substr(0,1)?"+"+a.replace(/[^\d]/g,…