0

I'm trying to set up some Selenium test cases for a website I've built. The site was built using Zurb Foundation to support mobile, and one of the fields I'd like to test for desktop is hidden from the mobile view using the hide-for-medium-down class. This seems to hide it from the HTMLUnitDriver as well--the element is there when I use findElement, but when I use .getText() it's blank. Any suggestions on how to access that data with the HTMLUnitDriver? Is there another way to hide the divs from mobile that will make them accessible? Anyone else run into this issue?

The code is below. pageName return the H1, but when I try to get the text it's blank.

<div id="main-content" class="grid_10 landing large-10 medium-12 columns">
    <div class="grid_10 alpha medium-12 hide-for-medium-down">
        <h1>Title</h1>
        <img src="/hr.jpg" alt="">
    </div>
<div>

Code to retrieve element on page

WebElement pageName = driver.findElement(By.xpath("//div[@id='main-content']/div/h1"));
pageName.getText();
shri046
  • 1,148
  • 11
  • 12
AllisonAnn
  • 61
  • 1
  • 5
  • Not quite sure what exactly your test case is but instead of `.getText()` method that returns only visible HTML, you could use `.getAttribute("innerHTML")`. – shri046 Nov 14 '14 at 16:51
  • I tried that as well and it's blank too. When I debug I can look in the element and the "first child" is the text that I'm looking for, but all my calls against the element: getText, getAttribute("innerHTML") come back blank. I tried clicking the element first as well, but that didn't do anything. I updated the question with the method call that's coming back null. – AllisonAnn Nov 14 '14 at 17:16
  • Can you please post the HTML snippet in question and the code you are using to obtain the text? – shri046 Nov 14 '14 at 17:18
  • What happens if you try to get innerHTML with the following `driver.findElement(By.xpath("//div[@id='main-content']"));`? Does that return any HTML at all? – shri046 Nov 14 '14 at 17:32
  • Yup, I did that too. I get everything that's not wrapped in the hide-for-medium-down class. I further tested by retrieving a div by id that had the same class. I also get no text there. I'm fairly sure that's what the problem is (given that Selenium won't return content for hidden divs) I'm just not sure what to do about it! – AllisonAnn Nov 14 '14 at 18:51
  • I wonder if it is a limitation of HTMLUnitDriver. Have you tried using a different driver, Firefox for example? – shri046 Nov 14 '14 at 19:00
  • Hey, now we're getting somewhere! The firefox driver does find it. I'm off to see if the HTMLDriver recognizes classes that only show small etc. I'll let you know how it goes. – AllisonAnn Nov 14 '14 at 19:27

1 Answers1

0

Couple of things you might want to take a further look at. This SO post indicates a similar issue with HTMLUnitDriver. Check if you are initializing the driver with javascript enabled

HtmlUnitDriver driver = new HtmlUnitDriver();
driver.setJavascriptEnabled(true);

The other thing to note is instead of trying to get the innerHTML attribute, execute the javascript code mentioned in the post. Don't think it would really change anything but worth a shot.

Community
  • 1
  • 1
shri046
  • 1,148
  • 11
  • 12