Home > Software engineering >  Not able to locate an element within the IFrame (Selenium)
Not able to locate an element within the IFrame (Selenium)

Time:10-05

So i have create successful test of adding a card to my profile and now i would like ot verify if card was added so i would like to check if "Your card was successfully added" text appeared, however for some kind of a reason the className not located.

This is an iframe however i do enter it to add the card details and submit them, so trying to understand why it cant locate the follow:

enter image description here

Adding card

public void addCard(Card card) {
        switchToCardIframe()
                .fillInCardNumber(card.getCardNumber())
                .fillInCardHolder(card.getCardHolderName())
                .fillInExpiryDate(card.getExpiryDate())
                .fillInCvv(card.getCvv())
                .clickOnToggle()
                .clickOnAddButton();
    }

This is how i locate the text

@FindBy(css = "payment-state-message")
private WebElement cardAddedIndicator;

Test

@Test
public void shouldSuccessfullyAddCard() {
    myCards.addCard(cardData());
    assertTrue(myCards.getCardAddedIndicator().isDisplayed());
}

I have also tried to check if another element displayed by escaping the iframe but no luck so any help appreciated

driver.switchTo().defaultContent()

enter image description here

I have also attempted Thread.sleep() for 10 seconds before taking getting the text from the css locater but not luck

Here is the base page where wait initialized

public class BasePage {

    public WebDriver driver;
    protected Wait wait;

    public BasePage(WebDriver driver) {
        initializePage(driver);
        this.wait = new Wait(driver);
    }

    final protected void initializePage(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(new AjaxElementLocatorFactory(driver, 20), this);
    }
}

CodePudding user response:

Your css is wrong, so instead of

@FindBy(css = "payment-state-message")
private WebElement cardAddedIndicator;

try this

@FindBy(css = ".payment-state-message")
private WebElement cardAddedIndicator;

also do not check for visibility by .isDisplay

rather have a text method called on it. Put some delay/wait as well.

String actualMessage = myCards.getCardAddedIndicator().getText();
String expectedMessage = "Your card was successfully added"

then assert these two based on their actual and expected behavior .

CodePudding user response:

Conclusion :

When there is an iframe form and you submit it, it does not mean that its still the same iframe. In my case when I submitted the form then iframe disappeared however in order for for the element to be located I had to perform driver().switch().parentFrame()

FYI: First always check your locators and make sure they are right.

  • Related