【发布时间】:2016-03-11 07:19:12
【问题描述】:
我有特定的情况,从谷歌浏览器的 Inspect 元素我有这个:
<span class="markup--quote markup--p-quote is-other" name="anon_7ee3d25bf521" data-creator-ids="anon">If what you care about — or are trying to report on — 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 selector","selector":"[data-creator-ids='anon']"} (会话信息: chrome=48.0.2564.116)(驱动程序信息:chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),平台=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>
【问题讨论】:
-
有问题的元素总是跨度吗?
-
是的,它总是跨度
-
元素是否加载了 ajax ?也许你需要在定位元素之前添加一些
wait -
我尝试了答案中给出的两种解决方案,都对我有用....
标签: c# selenium selenium-chromedriver