硒发现隐藏元素C#

古典312

根据Google Chrome中的Inspect元素,我有一个特定的情况:

 <span class="markup--quote markup--p-quote is-other" name="anon_7ee3d25bf521" data-creator-ids="anon">If what you care about&#8202;—&#8202;or are trying to report on&#8202;—&#8202;is impact on the world, it all gets very slippery. You’re not measuring a rectangle, you’re measuring a multi-dimensional space. You have to accept that things are very imperfectly measured and just try to learn as much as you can from multiple metrics and anecdotes.</span>

我想要基于文本,data-creator-ids="anon"但是问题是当我单击“查看页面”源时,这是完全隐藏的。

我尝试这样:

IWebElement ell = driver.FindElement(By.CssSelector("[data-creator-ids='anon']"));

            MessageBox.Show(ell.GetAttribute("innerHTML"));

问题:

未处理OpenQA.Selenium.NoSuchElementException
HResult = -2146233088消息=没有这样的元素:无法找到元素:{“方法”:“ css选择器”,“选择器”:“ [data-creator-ids ='anon']”}} (会话信息:chrome = 48.0.2564.116)(驱动程序信息:chromedriver = 2.21.371459(36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform = Windows NT 10.0 x86_64)源= WebDriver

我也尝试通过类名,但同样的问题。是否有任何技巧可以获取此文本?

我也有一个按钮有同样的问题,我只需要Id,但是按钮对html源是完全隐藏的:

<button class="button button--chromeless" data-action="select-anchor" data-action-value="3331">Top highlight</button>
马蒂斯

如果您看一下这篇有趣的文章,您会发现使用CSS-Selectors时需要定义一个元素类型:

element[attribute(*|^|$|~)='value']

基于此,您要做的就是将正确的元素添加到选择器中。在您的情况下,一个span元素:

IWebElement ell = driver.FindElement(By.CssSelector("span[data-creator-ids='anon']"));
MessageBox.Show(ell.GetAttribute("innerHTML"));

因此,而不是"[data-creator-ids='anon']"您应该使用"span[data-creator-ids='anon']"这应该产生正确的结果。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章