简体   繁体   中英

Selenium find hidden element C#

I have specific situation, from Inspect element in Google Chrome I have this:

 <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.

I try this:

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

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

Problem:

OpenQA.Selenium.NoSuchElementException was unhandled
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

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:

<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:

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']" . This should yield the correct result.

it may be better to try to get by XPath, rather than CSSSelector. This may well take you a step closer:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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