Selenium webdriver 从注释中查找字符串 (C#) [英] Selenium webdriver find string from a comment (C#)

查看:49
本文介绍了Selenium webdriver 从注释中查找字符串 (C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个网站上有这样一行:

I have in a website a line like this:

<!--<td class="points">100.000</td>-->

我怎样才能把那个100.000"放到一个字符串中?我在 c# 中尝试过这样的事情:

How can I take that "100.000" and put it in a string? I tried something like this in c#:

string points = driver.FindElement(By.XPath("/html/body/div[2]/div[2]/table[1]/tbody/tr[2]/comment()")).Text;

但是当我尝试获取该数字并将其放入字符串时出现这样的错误:

But I get an error like this when I try to take that number and put in the string:

"xpath 表达式的结果/html/body/div[2]/div[2]/table[1]/tbody/tr[2]/comment()"是:[对象评论].它应该是一个元素."

"The result of the xpath expression "/html/body/div[2]/div[2]/table[1]/tbody/tr[2]/comment()" is: [object Comment]. It should be an element."

有什么想法可以做到这一点吗?

Any ideas to do this?

推荐答案

不,你不能用 FindElement() 直接定位评论 - 它只适用于实际元素 - 而不是文本或评论节点.

No, you cannot directly target comments with FindElement() - it will only work for actual elements - not the text or comment nodes.

你需要另一种方式.这个想法是首先使用 driver.PageSource 获取页面的完整源代码,或者使用 elm.GetAttribute("innerHTML")elm.GetAttribute("outerHTML") 其中 elm 可以在您的案例中定义为包含注释节点的行:

You need another way. The idea is to first either get the complete source of the page using driver.PageSource or to get the inner or outer HTML of an element that contains the comment node using elm.GetAttribute("innerHTML") or elm.GetAttribute("outerHTML") where elm could defined in your case as the row containing the comment node inside:

IWebElement elm = driver.FindElement(By.XPath("/html/body/div[2]/div[2]/table[1]/tbody/tr[2]"));

然后,您可以通过多种方式从 HTML 字符串中提取注释.我认为最好的方法是使用 HTML 解析器,例如 HTML 敏捷包:

Then, you have several options for how to extract the comment from an HTML string. The best, I think, is to use an HTML parser, like HTML Agility Pack:

这篇关于Selenium webdriver 从注释中查找字符串 (C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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