1

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.

Scherling
  • 1,350
  • 2
  • 13
  • 23
Muratcan
  • 235
  • 2
  • 6
  • 18
  • That looks correct. Are you sure the page is containing the given HTML code at the time you call Selenium? Is there maybe any ajax loading which makes this part of the page not being here at first. – LaurentG Feb 20 '14 at 12:07
  • or is there an iframe which you have to switchTo first? – tr4pt Feb 20 '14 at 12:07
  • With Java I was having this issue with a BR tag returning empty. Instead of `.getText()` I used `.getAttribute("innerHTML")` which will then return what I was looking for, including any HTML that is invisible or text that is hidden. – jsherk Jun 09 '14 at 16:08

1 Answers1

3

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)
Scherling
  • 1,350
  • 2
  • 13
  • 23
  • Thank you for that but there is one more think this will be a loop i have to do this for 3000 telefoon nummers, is there any approach using itemprop="telephone" attribute. – Muratcan Feb 20 '14 at 12:16
  • Well, there are ways to do this, however, is there any chance there's a connection between the element ID and the number it is in the list? If there is, I'd just generate scripts and execute them in a for loop to get all the data I need. If you use jquery, "$('span[itemprop=telephone]')" will return all elements of that type. (which you will then need to iterate over) – Scherling Feb 20 '14 at 12:25
  • I am still bussy with this problem i can not use Jquery, i tried this code String script = "return document.getElementByXPath('//span[@itemprop='telephone']').getText();"; String telno1 = ((JavascriptExecutor) driver).executeScript(script).toString(); without any solution – Muratcan Feb 20 '14 at 13:57
  • @Muratcan, this is because there is no such thing as `document.getElementByXPath`.... http://stackoverflow.com/questions/10596417/is-there-a-way-to-get-element-by-xpath-in-javascript ...as an aside, you do not need to *find* the element using JavaScript. You can find it using Selenium's own methods *first* and *then* pass it through. – Arran Feb 20 '14 at 14:38