ceddyyy
ceddyyy

Reputation: 27

Cannot find context with specified id in Java - Selenium webdriver

I want to click an element inside a iframe

code trial :

driver.switchTo().frame("payment_page");
WebElement cardType = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"AMEX-paymenttype\"]")));
cardType.click();

Error Msg: org.openqa.selenium.support.ui.ExpectedConditions findElement WARNING: WebDriverException thrown by findElement(By.xpath: //*[@id="AMEX-paymenttype"])

Upvotes: 1

Views: 1577

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193058

As per the Best Practices before you switch to any frame you need to induce WebDriverWait for the frame to be available and switch to it. Once you have switched to the desired frame further as you are invoking click() method so instead of using the ExpectedConditions method visibilityOfElementLocated you need to use elementToBeClickable as follows :

new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("payment_page")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"AMEX-paymenttype\"]"))).click();

Upvotes: 1

Related Questions