0

Here is the code that I have written.I have tried adding thread.sleep() but it still doesn't work also tried with chromedriver but same result

package com.thinksys.frames;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Iframes 
{
    public static void main(String[] args) 
    {
        System.setProperty("webdriver.gecko.driver","C:\\Users\\thinksysuser\\Downloads\\geckodriver-v0.18.0-win64\\geckodriver.exe");

        WebDriver driver = new FirefoxDriver();
        driver.get("https://www.irctc.co.in/eticketing/loginHome.jsf");

        WebElement e = driver.findElement(By.id("google_ads_iframe_/37179215/DFP_NGET_01_HomePage_RHS_ATF_479x266_ENG_0"));

        driver.switchTo().frame(e);

        driver.findElement(By.xpath(".//*[@id='image-11']/a/img")).click();
    }
}
NarendraR
  • 7,577
  • 10
  • 44
  • 82

2 Answers2

1

It might caused by the special characters in the <ifram> id. Using partial id will provide two matches, so I suggest you use two portions of the name attribute

WebElement frame = driver.findElement(By.cssSelector("[name*='google_ads_iframe'][name*='DFP_NGET_01_HomePage_RHS']"));
driver.switchTo().frame(frame);

Edit

The images rotates, each on is visible for a few seconds only. To click on a specific image you need to wait for it to be visible. You can use explicit wait for it

WebDriverWait wait = new WebDriverWait(driver, 60, 50);
wait.until(ExpectedConditions.visibilityOfElementLocatedBy.xpath(".//*[@id='image-11']/a/img"))).click();

This will pole the DOM every 100 milliseconds until the image is visible or the time is up (60 seconds).

Guy
  • 46,488
  • 10
  • 44
  • 88
  • I did try the AND operation but it still doesnt work.....Is there any chance that it is a browser or system problem because I did face a system problem last time –  Jul 19 '17 at 11:13
  • @AkashdeepSingh `cssSelector` doesn't have `AND`, and in `xpath` its with lowercase `and`. Please try exactly the code I provided. – Guy Jul 19 '17 at 11:16
  • I was talking about xpath only, let me try your code –  Jul 19 '17 at 11:20
  • I was really positive about this one but still not working –  Jul 19 '17 at 11:22
  • 1
    @AkashdeepSingh What didn't work? the switch or the clicking after that? and what exactly happened? *not working* is not really informative. – Guy Jul 19 '17 at 11:31
  • Sorry about that, I am not able to execute the click part –  Jul 19 '17 at 11:50
  • which MP means that my code cannot switch to the frame –  Jul 19 '17 at 11:52
  • Thanks, I should have taken that into account –  Jul 19 '17 at 12:05
  • if we can calculate the amount of time after which that picture repeats then we can use Thread.sleep too, Correct? –  Jul 19 '17 at 12:11
  • @AkashdeepSingh Theoretically yes, but it will work only if the images are always loaded in the same order and the number of images never changes. If the target image is visible in different intervals it won't be possible, and you will have to update the code every time a image is removed or added. – Guy Jul 19 '17 at 12:15
  • True that, Thank you so much –  Jul 19 '17 at 12:17
0

There is variations on browsers to display ads. I have opened in Firefox and not able to see the ad while its displaying in chrome. and the scenario while i do open the Chrome or Firefox browser using script there is no ad section.

based on this scenario you can check that weather frame is available or not if available then switch into it and wait till the image you gonna click get visible then click on it

You can try this way :

WebDriverWait wait = new WebDriverWait(driver, 120);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(driver.findElement(By.xpath("//iframe[starts-with(@id,'google_ads_iframe')][@title='3rd party ad content']"))));

wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//div[@id='image-11']/a/img")))).click();

or

WebDriverWait wait = new WebDriverWait(driver, 120);
List <WebElement> adFrame = driver.findElements(By.xpath("//iframe[starts-with(@id,'google_ads_iframe')][@title='3rd party ad content']"))
   if(adFrame.size()>0)
    {
        driver.switchTo().frame(adFrame.get(0));
        wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//div[@id='image-11']/a/img")))).click();
    }
    else
    {
        System.out.println("Sorry there is no ads");
    }
NarendraR
  • 7,577
  • 10
  • 44
  • 82