no such element: Unable to locate element: {"method":"css selector","selector":"input#btnshow"}

Viewed 59

I am a beginner in selenium, I am using java for that, I am getting a error saying Unable to locate element although that element is present in the web page.

Java Code:

    WebDriver dChromedriver=new ChromeDriver();
    dChromedriver.get("https://erp.mitwpu.edu.in/");
    dChromedriver.manage().window().maximize();
    WebElement  txtPassword=dChromedriver.findElement(By.id("txtPassword")) ;
    WebElement txtUserId=dChromedriver.findElement(By.id("txtUserId")) ;
    txtUserId.sendKeys("S1032200787");
    txtPassword.sendKeys("FynS@3XbZH6ZMWH");
        //
        WebDriverWait w =new WebDriverWait(dChromedriver, Duration.ofSeconds(30));
         
 
 w.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='ReCaptchContainer']")));
        dChromedriver.findElement(By.xpath("//div[@id='ReCaptchContainer']")).click();
//      Thread.sleep(3000);
        w.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("label#lblMessage")));
        WebElement success_lablElement=dChromedriver.findElement(By.cssSelector("label#lblMessage"));
        Assert.assertEquals(success_lablElement.getText(),"Success" );
        dChromedriver.findElement(By.id("btnLogin")).click();
        WebElement resultWebElement=dChromedriver.findElement(By.xpath("//nav[@class=\"mt-2\"]/ul/li[12]/a[@href=\"Examination/Report/StudentGradeCardDetail.aspx?MENU_CODE=Web_Result\"]"));
        resultWebElement.click();
        Thread.sleep(30000);
    dChromedriver.findElement(By.cssSelector("input#btnshow")).click();

html code:

 <div class="labelinfo col-md-12 col-sm-7 col-xs-12 padd6">                                  <input type="submit" name="btnshow" value="Show" onclick="return funValidation();" id="btnshow" class="btn btn-primary" style=""> &nbsp;&nbsp; 
<input type="submit" value="Show" onclick="return funValidation();" id="btnshow" class="btn btn-primary" style="">
 </div>
1 Answers

The element Show button is inside an iframe. In order to access the element, you need to switch to iframe first and then you can access the element.

Use WebDriverWait() and wait for frameToBeAvailableAndSwitchToIt() and wit for elementtobeclickable() to click on the Show button.

WebDriverWait w =new WebDriverWait(dChromedriver, Duration.ofSeconds(30));

w.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("FrameContent")));

w.until(ExpectedConditions.elementToBeClickable(By.id("btnshow"))).click();

To jump out from iframe you need to use this code.

dChromedriver.switchto().defaultcontent();

Snapshot iframe:

enter image description here

Related