我想在 selenium v​​ba 中用 javascript 填充数据 [英] I want to fill data with javascript in selenium vba

查看:11
本文介绍了我想在 selenium v​​ba 中用 javascript 填充数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个必须填写的预填充数据的网络表单.我想用 javascript 做到这一点.我不确定会写代码.

I have a web form of pre populated data which i have to fill. I want to do this with javascript. I am not sure to write the code .

Public Sub ABC()
    Dim document As HTMLDocument
    Dim bot As New WebDriver
    Dim  cSCRIPT,m
    m=58
    bot.Start "chrome", ""
    bot.Get "https://XYZ"
  cSCRIPT = "document.getElementByXPath("//td[contains(text(),'XXXX')]/following-sibling::td[5]").value='" & m & "'"
 bot.ExecuteScript cSCRIPT

但是我的代码不起作用.如何借助 XPath 在 selenium VBA 中编写代码以填充 Web 表中的数据.

But my code do not work. How can I code in selenium VBA with the help of XPath to fill data in Web table.

推荐答案

我无法在 VBA 中对此进行测试,但已从有效的 Python 中进行了翻译.我使用 evaluate 来处理 xpath 匹配.

I can't test this in VBA but have translated from python which worked. I use evaluate to handle the xpath matching.

Document.evaluate() 函数的定义如下:

var xpathResult = document.evaluate(
  xpathExpression,
  contextNode,
  namespaceResolver,
  resultType,
  result
);

第一个参数是 xpath 表达式.然后 contextNode 基本上是将 xpath 应用到的节点,即 document 在这种情况下.namespaceResolvernull 因为没有使用命名空间前缀(它是一个 html 文档).我将 resultType 指定为 XPathResult.ANY_TYPE 以便从表达式中获取自然类型.对于 result 参数,我传递 null 以创建一个新的 XPathResult.

The first argument is the xpath expression. Then contextNode is basically the node to apply the xpath to i.e. document in this case. namespaceResolver is null as no namespace prefixes are used (it's an html document). I specify resultType as XPathResult.ANY_TYPE so as to get the natural type from the expression. For result argument I pass null so as to create a new XPathResult.

返回类型是object.我使用 iterateNext 到达 input 元素,然后用 input.value = "abc"; 分配视觉值,用 <分配实际值code>input.setAttribute('value', 'xyz');.

The return type is object. I use iterateNext to get to the input element and then assign the visual value with input.value = "abc"; and the actual with input.setAttribute('value', 'xyz');.

我正在使用一个可测试的示例,但您会根据您的 html 进行修改,这意味着您的 xpath 将是

I am using a testable example but you would amend as per your html which means your xpath would be

//td[contains(text(),'XXXX')]/following-sibling::td[5]/input

<小时>

VBA:

Dim s As String
s = "var td = document.evaluate(""//tr[contains(td/text(), 'Angelica Ramos')]/td[2]/input"", document, null, XPathResult.ANY_TYPE, null);" & _
    "var input = td.iterateNext();" & _
    "input.value = 'abc';" & _
    "input.setAttribute('value', 'xyz');"

bot.Get 'https://datatables.net/examples/api/form.html'
bot.ExecuteScript s

<小时>

Python 原文:

bot.get('https://datatables.net/examples/api/form.html')

s = '''var td = document.evaluate("//tr[contains(td/text(), 'Angelica Ramos')]/td[2]/input", document, null, XPathResult.ANY_TYPE, null);     
       var input = td.iterateNext();
       input.value = 'abc';
       input.setAttribute('value', 'xyz');
'''
bot.execute_script(s)

<小时><小时>

参考文献:



References:

  1. https://developer.mozilla.org/en-US/docs/Web/API/Document/evaluate
  2. https://developer.mozilla.org/en-US/docs/Web/API/Document/evaluate#Result_types
  3. https://javascript.info/object
  4. https://developer.mozilla.org/en-US/docs/Web/API/XPathResult/singleNodeValue
  5. https://developer.mozilla.org/en-US/docs/Web/API/XPathResult/iterateNext

这篇关于我想在 selenium v​​ba 中用 javascript 填充数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆