1

Hellow guys, please guide me to solve issue.

I am able to access all fields inside the iframe which is in div, I want to close iframe but I am unable to access (X) button.

The close button is outside iframe and inside div.

Here is my code:

To switch into iframe from main window:

BaseClassOne.driver.switchTo().frame(BaseClassOne.driver.findElement(By.xpath("//*[@id='Dealership quote Internal']/iframe"))); 

To access iframe element:

BaseClassOne.driver.findElement(By.xpath("//*@id='txtDealershipRef']")).sendKeys("XYZ090123");

I tried below mention code to close modal popup window:

BaseClassOne.driver.findElement(By.tagName("a")).click();// throwing no such element exception

BaseClassOne.driver.close();// this is closing browser instance

BaseClassOne.driver.switchTo().defaultContent();
// modal pop-up is not closing hence not able to access main window element

Please guide me.

Thanks in advance.

enter image description here

6
  • What do you mean by modal pop-up? Are you referring to what is getting displayed in the iframe? Commented Nov 2, 2016 at 6:38
  • yes what is display inside frame. Commented Nov 2, 2016 at 7:17
  • Please view image which give you clear idea of situation. Commented Nov 2, 2016 at 7:20
  • What exceptions do you get? Commented Nov 2, 2016 at 7:21
  • When I am trying to click (X) button, BaseClassOne.driver.findElement(By.tagName("a")).click() , selenium throwing error no such element. The problem is current focus is in frame while close(X) button is out side the frame. Commented Nov 2, 2016 at 7:26

2 Answers 2

1

According to the above comments and discussions, I feel that you have to switch back to the default frame and then try to click on the close button.

driver.switchTo().defaultContent();

Sign up to request clarification or add additional context in comments.

1 Comment

yes, I switchback to main div and then i try to close it and it works. Thanks
0

There seems to be an issue with the code sequence. Considering the close link is not contained in the iframe you have to switch to default content first and then click on close before proceeding. Try the following code:

//Switch to frame and perform actions
BaseClassOne.driver.switchTo().frame(BaseClassOne.driver.findElement(By.xpath("//*[@id='Dealership quote Internal']/iframe"))); 
BaseClassOne.driver.findElement(By.xpath("//*@id='txtDealershipRef']")).sendKeys("XYZ090123");

//switch to default content (to access elements outside the frame)
BaseClassOne.driver.switchTo().defaultContent(); 

//click on close 
BaseClassOne.driver.findElement(By.xpath("//a[@class='close-window']")).click();

Note: the identifier for the close button has been changed in the code here to use the properties from the HTML sinceBy.tagName("a") which is a part of the code in the question could possible have many matching nodes which are higher up in the HTML hierarchy.

2 Comments

Thank you sai for your reply, I tried but it is giving error org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression ///a[@class='close-window'] because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '///a[@class='close-window']' is not a valid XPath expression.
Thank you Sai, it works fine. I used actual xpath instead of ///a[@class='close-window']" and it works for me. Thank you for your suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.