【问题标题】:Fast writing in a textBox with selenium and python使用 selenium 和 python 在文本框中快速写入
【发布时间】:2019-02-19 12:22:57
【问题描述】:

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

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

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

【问题讨论】:

  • fill them faster 到底是什么意思?你的确切用例是什么?
  • 因为 12 秒后页面会重新加载,所以我需要更快地填充文本框
  • 尝试仅基于类一次查找所有元素:elements = driver.find_elements_by_xpath("//input[@class='string required']")。然后循环:for e in elements:sendkeys...
  • 您要在 12 秒内填写多少个字段?手动可以吗? 用例说明了什么?
  • 首先要找出需要很长时间的原因:find_element_by_xpathsend_keys。不同的问题。此外,由于您的 xpath 包含 ID,因此实际上不需要其他属性,因此您的 xpath 可以是"//input[@id='order_billing_name']"。但是,如果它始终是 ID,您可以改用 find_element_by_id,这样会更快。尽管仍然如此,但首先要弄清楚什么需要时间:查找或输入

标签: javascript python html windows selenium


【解决方案1】:

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

【讨论】:

  • 问题是做river.find_element_by_id("order_billing_name") 找不到文本框,因为这个原因我用xpath 做的
  • @Maria css 选择器 input#order_billing_nameinput[id='order_billing_name'] 应该可以工作
  • 它似乎有效,但如果我想“document.querySelector('.order_number_class').value='12323';”但是inste 12323我想发送一个python变量?
  • @Marià 因为你可以发送变量。例如,检查如何处理我的答案更新中的所有字段。
  • @Marià 我不明白“javascript 代码之类的警报”是什么意思。使用execute_script你可以发送任何javascript代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-11
  • 1970-01-01
  • 2013-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多