I am automating job applications on Naukri.com using Selenium WebDriver in Java.
My code successfully navigates to the job page and opens the job details, but I am unable to click on the 'Apply' or 'Interested' buttons. The buttons exist on the page, but Selenium throws ElementNotInteractableException or the click does nothing.
I have tried the following:
Using WebDriverWait and ExpectedConditions.elementToBeClickable.
Scrolling the element into view with JavascriptExecutor.
Using Actions class to move to the element.
Clicking via JavaScript: js.executeScript("arguments[0].click();", element);
Here is the relevant snippet:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement applyBtn = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Apply')]")));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", applyBtn);
Sometimes a chatbot overlay appears, which might block the button. I am not sure how to reliably handle it.
Question:
How can I reliably click the 'Apply' or 'Interested' buttons on Naukri.com job pages using Selenium Java, even if overlays like chatbots appear?
package nokrijob;
import java.time.Duration;
import java.util.List;
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
import base.BaseTest;
import publicMethod.PublicMethod;
public class Nokri extends BaseTest {
Actions action;
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20));
// XPath for pagination "Next" button
static String nextPagi = "//span[text()='Next']/parent::a";
// Scroll entire page to bottom
public void scrollToBottom(WebDriver driver) {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
}
// Scroll specific element to center of screen
public void scrollToElement(WebElement element) {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView({block:'center'});", element);
}
@Test
public void nokri() throws Throwable {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
// STEP 1: OPEN NAUKRI WEBSITE
driver.get("https://www.naukri.com/");
System.out.println("Naukri launched: " + driver.getCurrentUrl());
// STEP 2: LOGIN
String loginBtn = "//a[text()='Login']";
PublicMethod.click(driver, loginBtn);
WebElement email = wait.until(ExpectedConditions.visibilityOfElementLocated(
By.xpath("//label[text()='Email ID / Username']/following-sibling::input")));
email.sendKeys("
[email protected]");
WebElement pass = driver.findElement(By.xpath("//label[text()='Password']/following-sibling::input"));
pass.sendKeys("shivam1213");
driver.findElement(By.xpath("//button[text()='Login']")).click();
Thread.sleep(3000);
System.out.println("Login successful");
// STEP 3: SEARCH JOB
String searchBox = "//span[text()='Search jobs here']/following-sibling::button";
PublicMethod.click(driver, searchBox);
WebElement keyword = wait.until(ExpectedConditions.visibilityOfElementLocated(
By.xpath("//input[@placeholder='Enter keyword / designation / companies']")));
keyword.sendKeys("Automation Testing, Automation Tester, QA Automation, Selenium");
driver.findElement(By.xpath("//span[text()='Search']//ancestor::button")).click();
// STEP 4: EXPERIENCE SLIDER
String slider = "//div[@class='slider-Container']//div[contains(@class, 'handle')]";
PublicMethod.waitForElementVisible(driver, slider);
PublicMethod.handleSlider(driver, slider, -182);
Thread.sleep(2000);
// STEP 5: FRESHNESS FILTER
String jobTime = "//span[text()='Freshness']/parent::div/following-sibling::div//button";
WebElement latest = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(jobTime)));
latest.click();
WebElement last7days = wait
.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Last 7 days']/parent::a")));
last7days.click();
System.out.println("Filter applied: Last 7 days");
// STEP 6: SORT BY DATE
Thread.sleep(3000);
String date = "//button[@id='filter-sort' or @title='Recommended']";
WebElement sortBtn = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(date)));
sortBtn.click();
Thread.sleep(2000);
PublicMethod.getScreenshot(driver);
WebElement sortDate = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[@title='Date']/a")));
sortDate.click();
System.out.println("Sorted by Date");
// STEP 7: STORE PARENT WINDOW
String parentWindow = driver.getWindowHandle();
System.out.println("Parent Window: " + parentWindow);
Thread.sleep(4000);
// STEP 8: TOTAL PAGES
String lastPage = "(//div[@align='center']//a)[last()]";
int countPageN = Integer.valueOf(driver.findElement(By.xpath(lastPage)).getText());
// STEP 9: PAGINATION LOOP
for (int i = 0; i < countPageN; i++) {
System.out.println("=========== Page: " + (i + 1) + " ==========");
String jobXpath = "//div[@class='srp-jobtuple-wrapper']//div[contains(@class,'row1')]//a";
List<WebElement> jobs = driver.findElements(By.xpath(jobXpath));
int jobCount = jobs.size();
System.out.println("Total jobs: " + jobCount);
// STEP 10: LOOP THROUGH JOBS
for (int j = 1; j <= jobCount; j++) {
jobs = driver.findElements(By.xpath(jobXpath));
WebElement job = jobs.get(j - 1);
String jobTitle = job.getText();
System.out.println("\nProcessing Job " + j + ": " + jobTitle);
scrollToElement(job);
((JavascriptExecutor) driver).executeScript("arguments[0].click();", job);
Thread.sleep(3000);
// STEP 11: SWITCH TO CHILD WINDOW
String childWindow = null;
for (String handle : driver.getWindowHandles()) {
if (!handle.equals(parentWindow)) {
childWindow = handle;
driver.switchTo().window(childWindow);
System.out.println("Child window id: " + childWindow);
PublicMethod.getScreenshot(driver);
break;
}
}
// STEP 12: CLICK APPLY / INTERESTED BUTTON
try {
Thread.sleep(6000);
// Use ID for reliability
List<WebElement> applyButton = driver.findElements(By.id("apply-button"));
List<WebElement> interestButton = driver.findElements(By.id("walkin-button"));
WebElement btn = null;
if (!applyButton.isEmpty()) {
btn = applyButton.get(0);
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView({block:'center'});",
btn);
wait.until(ExpectedConditions.elementToBeClickable(btn));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", btn);
System.out.println("Clicked Apply button for: " + jobTitle);
} else if (!interestButton.isEmpty()) {
btn = interestButton.get(0);
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView({block:'center'});",
btn);
wait.until(ExpectedConditions.elementToBeClickable(btn));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", btn);
System.out.println("Clicked Interested button for: " + jobTitle);
} else {
System.out.println("No Apply/Interested button available for: " + jobTitle);
}
} catch (Exception e) {
System.out.println("Apply / Interested button not found for job: " + jobTitle);
PublicMethod.getScreenshot(driver);
}
// STEP 13: CLOSE CHILD WINDOW
if (childWindow != null) {
driver.close();
driver.switchTo().window(parentWindow);
} else {
driver.navigate().back();
}
}
// STEP 14: NEXT PAGE
List<WebElement> next = driver.findElements(By.xpath(nextPagi));
if (!next.isEmpty() && next.get(0).isDisplayed()) {
scrollToBottom(driver);
next.get(0).click();
System.out.println("Moving to next page");
Thread.sleep(4000);
} else {
System.out.println("No more pages");
break;
}
}
}
}