dev
dev

Reputation: 123

Unable to click on a button inside an iframe

Scenario:
Launch http://www.indiabookstore.net/.
Click on FB like button which is inside an iframe. (scroll down to see it)

Issue:
I'm able to switch to the iframe, but couldn't click on the button, as there is a NoSuchElementException

I tried giving relative as well as absolute xpaths, didn't work.

Upvotes: 3

Views: 20974

Answers (3)

Ripon Al Wasim
Ripon Al Wasim

Reputation: 37756

The following code should work for fb Like button:

WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.indiabookstore.net/");

driver.switchTo().frame(driver.findElement(By.xpath("//html/body/div[1]/div[11]/div[1]/div[2]/div[1]/div/span/iframe")));
WebElement fb = driver.findElement(By.xpath("//html/body/div/div/div/table/tbody/tr/td[1]/div/form/div/div[1]"));//fb Like button

Actions builder = new Actions(driver);
builder.moveToElement(fb).click().build().perform();
driver.switchTo().defaultContent();

Upvotes: 0

Uday
Uday

Reputation: 1484

I tried couple of times executing through WebDriver, but facebook like button is not coming up. So i tried for Twitter with below code, which worked for me.

WebDriver driver = new FirefoxDriver();
        driver.get("http://www.indiabookstore.net/");
        Thread.sleep(10000);
        driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@class='home-twitter']")));
        driver.findElement(By.xpath("//span[@class='label']")).click();

Watch the above said video else watch this video to know more about switch between frames: https://www.youtube.com/watch?v=yYv_7-zYz4k

Upvotes: 1

Vivek Singh
Vivek Singh

Reputation: 3649

Try out this way...

driver.switchTo().frame(
        driver.findElements(By.tagName("iframe")).get(2));
new WebDriverWait(driver, 20).until(
    ExpectedConditions.elementToBeClickable(By
        .xpath("(//span[.='Like'])[1]"))).click();

Upvotes: 3

Related Questions