How to handle Lazy Loading Webpage

Hi;
Can anyone help me to locate this element.There is a paticular link i want to click.
Firepath gives the xpath as “html/body/section/ul/li/article/div[2]/a/h3” but webdriver can’t find this xpath and it can’t find the css selector also–

I can’t locate it either by name,link or id.

 

Regards.

Train Asked on September 5, 2014 in Selenium WebDriver.
Add Comment
  • 5 Answer(s)
    Best answer

    HI Priya,

    Thanks for bringing this up, as it is something new to us as well.

    There is a new technology in the web developement, which is called Lazy Loading. This makes your web page lighter and load fast. How it works is that it loads the page only what is visible on the screen, and on scolling down  the page, it agains loads the rest of the page. So it loads only the visible part of the webpage, not the whole web page as a whole.

    So solution to you problem is to scoll down the web page where you element is visible. Make sure you do not scroll it down completely at a go, as if you directly reach to the bottom of the page, then again it will not load the middle web page.

    So your intrested element will be active only when you load that part of the page. Hope I made my point clear. Code for your solution:

    
    driver = new FirefoxDriver();
    
    driver.manage().window().maximize();
    
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    
    driver.get("URL");
    
    JavascriptExecutor jse = (JavascriptExecutor)driver;
    
    jse.executeScript("window.scrollBy(0,1400)", "");
    
    Thread.sleep(3000);
    
    driver.findElement(By.xpath("/html/body/div[2]/div[2]/div[2]/div[2]/main/section[4]/ul/li[2]/article/div[2]/a")).click();
    
    

    Or if you see the same element on Chrome browser, it populate some other details about the product, when you mouse hover that product, so at that time you can use the following code:

    
    driver = new FirefoxDriver();
    
    driver.manage().window().maximize();
    
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    
    driver.get("URL");
    
    JavascriptExecutor jse = (JavascriptExecutor)driver;
    
    jse.executeScript("window.scrollBy(0,1400)", "");
    
    Thread.sleep(3000);
    
    WebElement element = driver.findElement(By.xpath(".//*[@id='inner-wrapper']/div[2]/main/section[4]/ul/li[2]/article"));
    
    Actions action = new Actions(driver);
    
    action.moveToElement(element).build().perform();
    
    driver.findElement(By.xpath("/html/body/div[2]/div[2]/div[2]/div[2]/main/section[4]/ul/li[2]/article/div[2]/a")).click();
    
    

    Please mark your question as ANSWERED

    Best Regards,
    Lakshay Sharma

    Professor Answered on September 9, 2014.
    Add Comment

    Hi Priya,

    Is this element hosted inside an Iframe?

    Can you give us the site url?

    Thanks
    Virender

    Professor Answered on September 5, 2014.
    Add Comment

    Hi Virender,
    Actually I can’t post the site url here.Sorry for that.And this element is not hosted inside an Iframe.
    It’s same like here http://www.amazon.com/
    Imagine I want click on an product here.when I’m trying to catch xpath by firepath it gives these details.

    Xpath:html/body/section/ul/li/article/div[2]/a/h3

    <article class=”product”>
    <div class=”product-image-wrapper”>
    <div class=”product-title-wrapper”>
    <a class=”” href=”/film/1_-_frozen_(blu-ray)_(import)-25620″>
    <h3 class=”title”> 1 – frozen (Blu-ray) … </h3>
    <p class=”format”>(Blu-ray)</p>
    </a>
    <div class=”gradient-overlay”/>
    </div>
    Thanks.

    Train Answered on September 5, 2014.
    Add Comment

    Hi Priya,

    Here html/body/section/ul/li/article/div[2]/a/h3 is relative to the HTML page. from the HTML snippet you have give its quite hard to understand what is above Artcile tag. Inside the article that is from /article/div[2]/a/h3 everything seems find. It has to be html/body/section/ul/li/ this part that might be wrong.

    Thanks
    Virender

    Professor Answered on September 6, 2014.
    Add Comment

    Hi Lakshay;

    Thanks a lot for your response.  Actually I have used this :

    JavascriptExecutor jse = (JavascriptExecutor)driver;
    jse.executeScript("window.scrollBy(0,1400)""");

    The problem was with finding this xpath: .//*[@id='inner-wrapper']/div[2]/main/section[4]/ul/li[2]/article

    Wonder how did you find this xpath 🙂 Anyway I got the answer.Really thanks!!

    Train Answered on September 9, 2014.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.