Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium Webdriver unable to get elements in Modal

I am writing a Selenium Webdriver script that is supposed to click on a Link and then this Modal Window pops up.

Modal Window on the page

When I try to access card number field (//input[@id=pan]), I get No such element found exception org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"pan"}

Input control I want to get hold of

This is the code I have tried with no luck:

WebElement modal = driver.findElement(By.xpath("//div[@class='ute-pay-now-modalContent']"));
driver.switchTo().frame(modal);
WebElement el =  driver.findElement(By.xpath("//input[@id='pan']"));

Also tried this:

WebElement modal = driver.findElement(By.className("ute-pay-now-modalContent"));
driver.switchTo().frame(modal);
WebElement el =  driver.findElement(By.xpath("//input[@id='pan']"));

Also tried this:

WebDriverWait block = new WebDriverWait(driver,10);
WebElement modal = block.until(ExpectedConditions.visibilityOfElementLocated(By.className("ute-pay-now-modalContent")));
WebElement pan;
pan    = modal.findElement(By.id("pan"));

Also tried this:

driver.switchTo().defaultContent();

Also tried this:

driver.switchTo().activeElement();

Can someone please help suggest me how to resolve this issue?

like image 943
rohit12sh Avatar asked Mar 19 '17 03:03

rohit12sh


People also ask

What could be the possible reason of getting unable to locate element?

We may encounter the error - unable to locate element while working with Selenium webdriver. This leads to NoSuchElementException. This type of exception is thrown when there is no element on the page which matches with the locator value. Check if there is any syntax error in our xpath expression.

What happens when list of elements is not found in Selenium?

selenium. NoSuchElementException occurs when WebDriver is unable to find and locate elements. Usually, this happens when tester writes incorrect element bin the findElement(By, by) method. This exception is thrown even if the element is not loaded.

What is ContextClick ()?

ContextClick() Right-clicks the mouse at the last known mouse coordinates.


1 Answers

It seem that <div class="ute-pay-‌​now-modalContent"> contains iframe#sema with required input field. Try below code and let me know the result:

WebDriverWait block = new WebDriverWait(driver,10);
block.until(ExpectedConditions.visibilityOfElementLocated(By.className("ute-pay-now-modalContent")));
driver.switchTo().frame("sema");
WebElement pan;
pan = modal.findElement(By.id("pan"));
like image 182
Andersson Avatar answered Oct 15 '22 23:10

Andersson