Selenium VBA 不支持 XPath 的 findElement?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20117235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 00:33:49  来源:igfitidea点击:

findElement By XPath not supported in Selenium VBA?

vbaexcel-vbaxpathseleniumselenium-webdriver

提问by JimmyK

variableName = driver.findElement(By.XPath(".//*[@id='T_F2']/fieldset/div[1]/div/div[4]/span[2]"))

Running the above always seems to lead to the error:

运行以上似乎总是会导致错误:

enter image description here

在此处输入图片说明

Why is this? I always see other people using findElement By XPath. If it helps, I generated about half of my code using Selenium's 'record' feature. I then converted the code into 'VBA/Webdriver' before pasting it into Excel to use as a Macro.

为什么是这样?我总是看到其他人使用 findElement By XPath。如果有帮助,我使用 Selenium 的“记录”功能生成了大约一半的代码。然后我将代码转换为“VBA/Webdriver”,然后将其粘贴到 Excel 中用作宏。

What exactly is wrong with my code? I have used findElement a number of times before, so I'd have to guess that the problem is with the By.XPathpart of my code... Is there any way around this?

我的代码到底有什么问题?我之前已经多次使用 findElement,所以我不得不猜测问题出By.XPath在我的代码部分......有什么办法可以解决这个问题吗?

Edit: Even variableName = driver.findElementsByXPath(".//*[@id='T_F2']/fieldset/div[1]/div/div[4]/span[2]")leads to the error 'Invalid procedure call or argument' even though it looks fine to me.

编辑:即使variableName = driver.findElementsByXPath(".//*[@id='T_F2']/fieldset/div[1]/div/div[4]/span[2]")对我来说看起来不错,甚至会导致错误“无效的过程调用或参数”。

回答by Jerome Montino

Try:

尝试:

variableName = driver.findElementByXPath("//div[@id='T_F2']/fieldset/div[1]/div/div[4]/span[2]")

Notice that I removed the .in the beginning of the xPath and replaced *with div. Also, you're missing something at the end. You are just declaring the path here and not really getting a value.

请注意,我删除了.xPath 开头的 并替换*div. 此外,你最后错过了一些东西。您只是在这里声明了路径,并没有真正获得价值。

EDIT: Referring to just the xPath is not usually enough. Do you want to perform an action on it, get the text inside, the tagname, etc.?

编辑:仅提及 xPath 通常是不够的。您想对其执行操作,获取其中的文本、标记名等吗?

EDIT2: Testing to get the .Textattribute returns a "findElement By XPath not supported in Selenium VBA?" message.

EDIT2:测试获取.Text属性会返回“Selenium VBA 不支持通过 XPath 查找元素?” 信息。

回答by jlookup

Here's what works for me:

以下是对我有用的内容:

Dim variableName() as variant

variableName = driver.findElementsByXPath("//div[@id='T_F2']/fieldset/div[1]/div/div[4]/span[2]").getdata

Notice it's "find elements [plural] by XPath". This creates a two-dimensional array. variableName(1,1) will have the data you're looking for.

请注意,它是“通过 XPath 查找元素 [复数]”。这将创建一个二维数组。variableName(1,1) 将包含您要查找的数据。