Java - Selenium Firefox Driver - cannot click on link in iframe

1.2k Views Asked by At

Any help would be much appreciated.

I want my program to log into indeed.ca (this is working, as long as you enter correct user credentials), navigate to a specific job posting(working), click on the first orange apply button(working), an iframe pops up.

Then I want to click on the blue apply button in iframe that appears. "apply with a different resume?" link (if you are not logged in with indeed you won't see this.)

If you don't have an indeed account, and want to help, just try to click on any link in the pop-up iframe. Example: try to click "Create one now" link

The below code worked 2 weeks ago, but now it seems Indeed.ca made a minor change to site and code is broken

import java.io.IOException;
import java.util.ArrayList;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;


public class Test {
//IOException, InterruptedException, NoSuchElementException 

    public static void main(String[] args) throws IOException, InterruptedException, NoSuchElementException {

        System.setProperty("webdriver.gecko.driver", "C:\\Users\\Padoga\\Documents\\geckodriver-v0.18.0-win64\\geckodriver.exe");

        try {

        FirefoxDriver driver = new FirefoxDriver();

        driver.get("https://secure.indeed.com/account/login?service=my&hl=en_CA&co=CA");
        driver.findElement(By.xpath("//*[@id=\"signin_email\"]")).sendKeys("[email protected]");
        driver.findElement(By.xpath("//*[@id=\"signin_password\"]")).sendKeys("password");
        driver.findElement(By.xpath("//*[@id=\"loginform\"]/button")).click();  





        driver.navigate().to("https://ca.indeed.com/viewjob?jk=ff97666702741fef&q=marketing&l=Toronto%2C+ON&tk=1boluh7om5igq9ng&from=web");
//      int size = driver.findElements(By.tagName("iframe")).size();
//      System.out.println(size);
        Thread.sleep(3000);
        //click orange "apply now" button
        driver.findElement(By.xpath("//*[@id=\"apply-state-picker-container\"]/div[1]/span[1]")).click();

        Thread.sleep(3000);

        //don't believe this is working now - below used to switch to correct pop-up iframe
        driver.switchTo().frame(1);
        driver.switchTo().frame(0);

        //not working anymore -- click "apply with a different resume?" link
        driver.findElement(By.xpath("\"//*[@id=\\\"form_container\\\"]/div[2]/div[1]/div[1]/p/a\"")).click();

        //no longer reach below steps
        //click on resume "choose file" button and upload resume
        driver.findElement(By.id("resume")).sendKeys("C:\\Users\\Padoga\\resumes\\Resume.pdf");
        //click blue apply button
        driver.findElement(By.id("apply-div")).click();
        }
        catch(Exception e){
            //System.out.println(e.getMessage());
        }

//      driver.quit();

    }

}
2

There are 2 best solutions below

3
On

You need to switch first to perform any action inside iframe elements

The code to switch to element is :-

driver.switchTo().frame(0);

In above code 0 is a index. so it will swicth control to first iframe present in your DOM. You may need to change the index 1,2,.. so on . There another paramenetrs also to switch to frame without index

Basically, we can switch over the elements in frames using 3 ways.

  • By Index
  • By Name
  • Id By Web Element

Refer below URL for more information

http://toolsqa.com/selenium-webdriver/handling-iframes-using-selenium-webdriver/

https://www.guru99.com/handling-iframes-selenium.html

Hope it will help you :)

7
On

Try using below code and lets see if it works for you-

driver.switchTo().frame("page_frame");
driver.findElement(By.xpath("//*[@id="form_container"]/div[2]/div[1]/div[1]/p/a")).click();

update-

driver.switchTo().frame("indeedapply-modal-preload-iframe");
driver.findElement(By.xpath("//*[@id="form_container"]/div[2]/div[1]/div[1]/p/a")).click();

update 2-

I found an iframe inside another iframe. So, first you need to switch to the outer iframe then to inner iframe and then to the element.

Possible solutions-

driver.switchTo().frame("indeedapply-modal-preload-iframe");
driver.switchTo().frame(0);
driver.findElement(By.xpath("//*[@id="form_container"]/div[2]/div[1]/div[1]/p/a")).click();

OR

driver.switchTo().frame(0);
driver.switchTo().frame(0);
driver.findElement(By.xpath("//*[@id="form_container"]/div[2]/div[1]/div[1]/p/a")).click();