简体   繁体   中英

How to get text value of hidden span using selenium

I have lithe trouble with getting telefoon nummer from website. The span looks like,

<span itemprop="telephone" class="hidden" id="telefoon_28836_hidden">010 - 79 53 364</span> 

I have tried this code

telno = driver.findElement(By.xpath("//span[@itemprop='telephone']")).getText();

And i'm getting empty value.

This is how to source code looks like after web driver load the page.

<div style="float: left; clear: both; padding-right: 7px;">Adres:<br>&nbsp;</div>
                            <div itemtype="http://schema.org/PostalAddress" style="float: left; color:#c71d22; width: 400px;">
                                <span itemprop="streetAddress">Mathenesserweg 49A BG</span><br>
                                <span itemprop="postalCode">3027 HE</span> <span itemprop="addressLocality"> Rotterdam </span>
                            </div>
                                                            <div style="float: left; clear: left; width: 70px;">&nbsp;<br>Telefoon:</div>
                                <div style="float: left; color:#c71d22; width: 400px;">&nbsp;<br>
                                    <span id="telefoon_28836_visible"><a href="#" class="countStatistieken" id="telefoon_28836" style="color:#c71d22;">Klik hier</a></span>
                                    <span itemprop="telephone" class="hidden" id="telefoon_28836_hidden">010 - 79 53 364</span>
                                </div>

if i use

straat = driver.findElement(By.xpath("//span[@itemprop='streetAddress']")).getText();

I am getting "Mathenesserweg 49A BG" it's work fine but i think because of the class="hidden" of telefoon nummer i am getting empty value. Can somebody help me how to resolve this issue. Thank you.

Selenium is created with the intention of immitating user input. A user cannot see hidden elements, which is why you can't find it.

You should query directly on the document to find the element

String script = "return document.getElementById('telefoon_28836_hidden').innerHTML";
String telno = (String) ((JavascriptExecutor) driver).executeScript(script)

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