0

In selenium I successfully switch to an iFrame which contains a modal window:

driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@name='intercom-tour-frame']")))

In this iFrame is a close window button which is clicked "successfully" but the window does not close. By successfully I mean the button is found using the xpath and the action is completed without error in my code.

This is what I'm trying:

@FindBy(xpath = ("/html[1]/body[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/span[1]"))
private WebElement closeTestTourButton;

public newCampaignPage clickCloseTestTourButton(WebDriver driver)
{
    delay(5000);

    closeTestTourButton.click();
}

I've also tried:

public newCampaignPage clickCloseTestTourButton(WebDriver driver)
{
    delay(5000);
    Actions builder = new Actions(driver);
    builder.moveToElement(closeTestTourButton).build().perform();
    waitForElementAndClick(closeTestTourButton, driver);
    return this;
}

The test continues but fails as it tries to do an action but this is not possible due to the still open modal window.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
ratsstack
  • 1,012
  • 4
  • 13
  • 32

3 Answers3

2

Try clicking the button using the javascript, sometimes the events might not trigger with normal click.

public newCampaignPage clickCloseTestTourButton(WebDriver driver)
{
    delay(5000);
    JavascriptExecutor executor = (JavascriptExecutor)driver;
    executor.executeScript("arguments[0].click();", closeTestTourButton);
    return this;
}

I would suggest using the WebDriverWait rather delay in your script. Below is the implementation.

WebDriverWait wait = new WebDriverWait(driver, 10);

 WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id(<someid>)));
supputuri
  • 13,644
  • 2
  • 21
  • 39
  • Thanks for your reply. The JS successfully closed the window but immediately after that the test failed with org.openqa.selenium.JavascriptException: JavaScriptError: Document was unloaded. Any ideas? – ratsstack Sep 17 '19 at 07:54
1

Possibly you are switching and attempting to click() too early.

To click() on the close window button as the the desired elements are within an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it:

    new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@name='intercom-tour-frame']")));
    
  • Induce WebDriverWait for the desired element to be clickable.

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("/html[1]/body[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/span[1]"))).click();
    
  • But as you are using @FindBy presumably you are using PageFactory in PageObjectModel, so you won't be able to invoke WebDriverWait in conjunction with ExpectedConditions directly and you have to create a method. You can find a relevant detailed discussion in How to wait for invisibility of an element through PageFactory using Selenium and Java


Outro

Here you can find a relevant discussion on Ways to deal with #document under iframe

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks for your reply. Unfortunately this did not work. It "clicked" the button as before but the modal window didn't close. The js suggestion by supputuri worked in closing the window but immediately after that the test fails with org.openqa.selenium.JavascriptException: JavaScriptError: Document was unloaded. Any ideas? – ratsstack Sep 17 '19 at 07:53
  • @ratsstack There are a couple of things 1) You got a _absolute xpath_ which may turn your tests flaky. 2) You haven't shared with us the HTML, so we aren't sure if the _xpath_ you are using identifies the element uniquely 3) While you use _Java_ clients, using _JavascriptExecutor_ should be your last option but never the **first**. – undetected Selenium Sep 17 '19 at 08:02
  • agree with what you are saying re JS and also xpath. The xpath is correct as 1) I've tested via chropath and 2) the button is being "clicked" by the JS method. I am using abs xpath in order to ensure I am hitting the correct element. I'll make it more robust once I resolve this issue. – ratsstack Sep 17 '19 at 08:09
  • Sorry re HTML, unfortunately it is not something I can share easily as it contains info that is private. I'll see if I can manipulate it sufficiently so I can share it but it will be a lot of work :( – ratsstack Sep 17 '19 at 08:10
0

I don't like to answer my own questions but in this case this was the only solution that worked:

Actions builder = new Actions(driver);
builder.moveToElement(closeTestTourButton).build().perform();
builder.sendKeys(Keys.ENTER).perform();

Granted, this is not the most elegant solution but after two days of trying it was the only one that worked.

ratsstack
  • 1,012
  • 4
  • 13
  • 32