简体   繁体   English

Selenium 查找隐藏元素 C#

[英]Selenium find hidden element C#

I have specific situation, from Inspect element in Google Chrome I have this:我有特定情况,从 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>

I want get text based on data-creator-ids="anon" but problem is when I click View Page source this is totally hidden.我想根据data-creator-ids="anon"获取文本,但问题是当我单击查看页面源时,这是完全隐藏的。

I try this:我试试这个:

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

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

Problem:问题:

OpenQA.Selenium.NoSuchElementException was unhandled OpenQA.Selenium.NoSuchElementException 未处理
HResult=-2146233088 Message=no such element: Unable to locate element: {"method":"css selector","selector":"[data-creator-ids='anon']"} (Session info: chrome=48.0.2564.116) (Driver info: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Windows NT 10.0 x86_64) Source=WebDriver HResult=-2146233088 Message=no such element: Unable to locate element: {"method":"css selector","selector":"[data-creator-ids='anon']"} (Session info: chrome=48.0 .2564.116) (驱动程序信息: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Windows NT 10.0 x86_64) Source=WebDriver

I also tried by Class Name but same problem.我也按类名尝试过,但同样的问题。 Is there any trick to get this text?有什么技巧可以得到这个文本吗?

Also I have one button with same problem, I only need Id but button is totally hidden from html source:另外我有一个按钮有同样的问题,我只需要 Id 但按钮完全隐藏在 html 源中:

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

If you take a look at this interesting post , you'll notice that you need to define an element type when using CSS-Selectors:如果你看一下 这篇有趣的文章,你会注意到在使用 CSS-Selectors 时需要定义一个元素类型:

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

Based upon this, all you have to do is add the correct element to your selector;基于此,您所要做的就是将正确的元素添加到您的选择器中; in your case a span element:在您的情况下,跨度元素:

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

So instead of "[data-creator-ids='anon']" you should use "span[data-creator-ids='anon']" .因此,您应该使用"span[data-creator-ids='anon']"而不是"[data-creator-ids='anon']" "span[data-creator-ids='anon']" This should yield the correct result.这应该会产生正确的结果。

it may be better to try to get by XPath, rather than CSSSelector.尝试通过 XPath 获取可能比 CSSSelector 更好。 This may well take you a step closer:这可能会让你更近一步:

IWebElement ell = driver.FindElement(By.XPath("//span[@data-creator-ids='anon']"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM