0

Can someone give a better xpath of this div element, or help explain why I cannot seem to select the desired div?

This XPath does not work:

//div[starts-with(normalize-space(.),'Welcome to the Shipt Shopper') and @class='text']

Even though it gets highlighted in chrome developer tool, NoSuchElementException is thrown.

This is the case with all elements on the page

A snippet of the HTML from the page with the content that I am trying to target:

<div class="content-wrapper">
    <div class="content" style="padding-top: 116px;">                               
        <div class="media">         
          <div class="attachment" data-attachment="{&quot;image&quot;:&quot;https:\/\/images.typeform.com\/images\/29rsVwT3VF\/image\/default#.png&quot;,&quot;width&quot;:360,&quot;height&quot;:137,&quot;video_source&quot;:&quot;&quot;,&quot;video_id&quot;:&quot;&quot;}" style="width: 360px; height: 137px;">
<img src="https://images.typeform.com/images/29rsVwT3VF/image/default#.png" data-original="https://images.typeform.com/images/29rsVwT3VF/image/default#.png" style="width: 360px; height: 137px; display: inline;">
  </div>
</div>                      
        <div class="text" style="padding-top: 30px; margin-left: 0px;">
                                                    Welcome to the Shipt Shopper application! <br><br>Ready to get started?

        </div>
        <div class="button-wrapper" style="margin-top: 30px;">
          <div class="button general full enabled hover-effect" style="">Begin</div>
        <div class="button-text">press <strong>ENTER</strong></div>
      </div>                    
    </div>
</div>

enter image description here

  • Is the element dynamically injected? – Obsidian Age Feb 26 '18 at 01:17
  • the html is seen in the image – Sharon Jones Feb 26 '18 at 01:17
  • 1
    ...Yes, I'm asking if that section of HTML injected into the DOM by some external event, like a button click or popup. – Obsidian Age Feb 26 '18 at 01:18
  • no its not dynamically injected – Sharon Jones Feb 26 '18 at 01:18
  • 1
    If would be helpful if you posted the snippet of HTML, not just a screenshot from the developer tool. For instance, we could more easily determine whether the quotes part of the text. – Mads Hansen Feb 26 '18 at 02:31
  • Possible duplicate of [How to handle iframe in Selenium WebDriver using java](https://stackoverflow.com/questions/9942928/how-to-handle-iframe-in-selenium-webdriver-using-java) – JeffC Feb 26 '18 at 03:39
  • 1
    @SharonJones You will need to wait for the iframe to complete loading. Use the frameToBeAvailableAndSwitchToIt(String frameLocator) expected condition which will switch to the iframe automatically. Then use the query to find the welcome text. https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html#frameToBeAvailableAndSwitchToIt-java.lang.String- – Grasshopper Feb 26 '18 at 04:17

2 Answers2

1

The content that you are targeting is inside of an <iframe>.

I'm not familiar with how to configure Selenium, but it looks as if you may need to switch to that frame. Since it does not have an @id you may need to select by position:

driver.switchTo().frame(0) 

and then execute the XPath.

When you are done, jump back to the containing HTML page:

driver.switchTo().defaultContent();
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
0

As per the HTML you have shared the xpath which you have used is a valid one and is correct. But NoSuchElementException will be thrown when you will try to interact with it through Selenium.

To interact with the element through Selenium you can use the following xpath :

//div[@class='content-wrapper']/div[@class='content']//div[@class='text' and contains(normalize-space(), 'Welcome to the Shipt Shopper application')]

But looking at the HTML it seems your have to induce WebDriverWait for the element to be visible as follows :

WebElement welcomeShiptShopper = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='content-wrapper']/div[@class='content']//div[@class='text' and contains(normalize-space(), 'Welcome to the Shipt Shopper application')]")));

The webpage doesn't opens at my end. As other's have indicated, the WebElement might be within <iframe> tag, in that case you have to switch to the appropiate iframe first using either of the following methods first :

  • Switch through Frame Name

    driver.switchTo().frame("frame_name");
    
  • Switch through Frame ID

    driver.switchTo().frame("frame_id");
    
  • Switch through Frame Index

    driver.switchTo().frame(1);
    

Once you switch to the appropiate frame, now you can lookout for the WebElement with the suggested xpaths mentioned above.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352