Home > other >  My Xpath is correct & no iFrame and I can locate element in Chrome console but my program still fail
My Xpath is correct & no iFrame and I can locate element in Chrome console but my program still fail

Time:09-22

My Xpath is correct & no iFrame and I can locate element in Chrome console but my program still fails. I have used explicit wait also. Website: https://qrgo.page.link/8YEcD I am trying to locate the male gender radio button. I can locate the element in the same chromedriver console but still it shows no element found exception.

Code:-

package automation;

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class residence {

public static void main(String[] args) throws InterruptedException {
    // TODO Auto-generated method stub
    System.setProperty("webdriver.chrome.driver", ".\\lib\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.get("https://serviceonline.bihar.gov.in/resources/homePage/10/loginEnglish.htm");
    driver.findElement(By.xpath("//label[contains(text(),'General')]")).click();
    driver.findElement(By.xpath(("//p[contains(text(),'Residential')]"))).click();
    driver.findElement(By.xpath(("//div[@id='collapseOneOne']/div/p/a"))).click();
    
    /* Write Gender accordingly.Default is Male(M).(F) and (T)*/
    char gender='M';
    WebDriverWait wait=new WebDriverWait(driver,Duration.ofSeconds(30));
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='17290_1']")));
    if(gender=='M')
    {
        driver.findElement(By.xpath("//input[@id='17290_1']")).click();
    }
else if(gender=='F')
    {
        driver.findElement(By.xpath("//input[@id='17290_2']")).click();
    }
    else
    {
        driver.findElement(By.xpath("//input[@id='17290_3']")).click();
    }
}

}

Here is a screenshot of the chrome window in which the testcase run: Here also u can see that element is visible

Error message image

CodePudding user response:

When click on the link, it is opening a new tab, you need to switch to new tab before accessing the element.

package automation;

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class residence {

public static void main(String[] args) throws InterruptedException {
    // TODO Auto-generated method stub
    System.setProperty("webdriver.chrome.driver", ".\\lib\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.get("https://serviceonline.bihar.gov.in/resources/homePage/10/loginEnglish.htm");
    // Store the current window handle
    String parent_handle = driver.getWindowHandle();
    driver.findElement(By.xpath("//label[contains(text(),'General')]")).click();
    driver.findElement(By.xpath(("//p[contains(text(),'Residential')]"))).click();
    driver.findElement(By.xpath(("//div[@id='collapseOneOne']/div/p/a"))).click();
    
    // Switch to new window opened
    for(String winHandle : driver.getWindowHandles()){
       if(!parent_handle.equals(winHandle))
       {
        driver.switchTo().window(winHandle);        
       }    
     }
    
    /* Write Gender accordingly.Default is Male(M).(F) and (T)*/
    char gender='M';
    WebDriverWait wait=new WebDriverWait(driver,Duration.ofSeconds(30));
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[@for='17290_1']")));
    if(gender=='M')
    {
        driver.findElement(By.xpath("//label[@for='17290_1']")).click();
    }
else if(gender=='F')
    {
        driver.findElement(By.xpath("//label[@for='17290_2']")).click();
    }
    else
    {
        driver.findElement(By.xpath("//label[@for='17290_3']")).click();
    }
}

}
  • Related