How to handle (X) button inside iframe using selenium - selenium

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.

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();

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.

Related

How to validate the focus of the WebDriver while switching frames

I go to the frame on the site using the code
driver.SwithToFrame(0)
How can I find out now in focus the main site or frame?
To validate if the focus is on the Top Browsing Context or within the iframe, you can switch to the active_element and then print the outerHTML and you can use the following solution:
driver.switch_to.frame(0)
element = driver.switch_to.active_element
print(element.get_attribute("outerHTML"))

Selenium for VBA - element not found error

I have been using a code since long time , but recently there is a new banner up which is hiding the element that I am trying to click.
Attaching the snapshot of error. The only help I need is I need to click the hidden element( if the browser window is maximized the element is visible).
.
Please help me.
If what you have said is true then you can use the following to maximize the window before clicking:
driver.Window.Maximize
Other options include:
1) Removing the banner
2) Scrolling the element into view
Can't write anything decent for those last two as your code is an image and I don't have a full URL to test with. You also haven't included the relevant HTML.
The "div.container-fluid" element is blocking the button you are trying to click.
You could try some of the following (as being shown here Element MyElement is not clickable at point (x, y)... Other element would receive the click):
prolong the wait before the click
use javascript executor

Selenium and StaleElementReferenceException

I am using selenium 3.9.1 and java to automate testing of a web application. The web application has some dynamic content based on pressing of a button for example. The page refreshes whenever this button is clicked. A java script runs on button click and updates the DOM I think. At this time, when I try to access the button (which is visible on the page), I get a staleElementReferenceException.
Does Selenium automatically reload the DOM once it is changed? I am relatively new to selenium. I have researched into this and I have tried to refresh the page using driver.navigate().Refresh() to try to see whether this will solve the problem. It does not solve the issue.
Any pointers will be deeply appreciated.
Since the page has been refreshed, the button reference you have is to the button on the old page that no longer exists.
I'd say you need to get a new reference to the button on the refreshed page (eg call FindElementById).
If the page is refreshed all the items in the DOM are now stale. What this means is that all items found before the button press will have to be found again. Any attempts to use those items will more than likely be treated with a stale element exception.
However, if the button click mearilly affects items on the page without having to ask the webserver to give you a new page you could interact with the old items.
You could do something like this:
public void SaveAndAgainClick() throws Exception{
try{
clicksaveButton(); //method to click save button
WebElement someValue = driver.findElement(By.xpath("(//input[#name='someValue'])[1]"));
someValue.click();
}catch (StaleElementException e){
WebElement someValue = driver.findElement(By.xpath("(//input[#name='someValue'])[1]");
someValue.click();
}
}
If findElement gets staleElementError while looking for (//input[#name='someValue'])[1] then it will again try one more time in the catch block and most certainly find the element and clicks on it. Your test will pass if you follow this approach.
Here are the answers to your questions :
A java script runs on button click and updates the DOM I think : If you inspect the HTML of the element through Development Tools / Inspect Element the element attributes will reveal it all.
Consider the following HTML :
<input value="Click me" onclick="alert('Click!')" type="button">
In the given HTML as per the onclick attribute of this element, if you invoke click() method on the WebElement, an alert would be generated. Similarly the onclick attribute may invoke a JavaScript or Ajax which may bring-in/phase-out new/old elements from the HTML DOM
At this time, when I try to access the button I get a staleElementReferenceException : In this case you should induce WebDriverWait for the WebElement to be interactive before attempting to interact with the element. Else you may face either of the following exceptions :
StaleElementReferenceException
WebDriverException
ElementNotInteractableException
InvalidElementStateException
Does Selenium automatically reload the DOM once it is changed? Short answer, Yes it does.
Refresh the page using driver.navigate().refresh() : No invoking driver.navigate().refresh() wouldn't be a optimum solution as it may not invoke the intended JavaScript or Ajax properly. Hence the intended WebElement may not be interactive in a optimum way.

Click on link using selenium and webdriver

I have been trying to perform a selenium task on it:
In this page, there is a button which i have to click on it and then wait for 10 seconds. I did it like this:
Naviagation to page:
base.driver.navigate().to("http://suvian.in/selenium/1.7button.html");
Click on button:
//base.driver.findElement(By.xpath("/html/body/div[1]/div/div/div/div/h3[2]/a"));
base.driver.findElement(By.linkText("Click Me"));
This step fails
Wait for 10 seconds:
TimeUnit.SECONDS.sleep(waitTime);
Questions:
1-it fails on clicking on the button. Although, i asked to find the link both with xpath, and text it cannot find it?
2-Is my solution correct for make a delay on webdriver's activity?
Try Below code for clicking on the "Click Me" button, tried on my local:
driver.findElement(By.xpath("//div[contains(#class,'intro-message')]")).findElement(By.partialLinkText("Click Me")).click();
Explanation for the above code : Thumb rule is try to go from the parent element of the DOM. In the above post, your parent element for the button is div class = intro-message . Once the parent element is located, then next find the child elements. In your case it was the button with link text 'Click Me'.
//base.driver.findElement(By.xpath("/html/body/div[1]/div/div/div/div/h3[2]/a"));
base.driver.findElement(By.linkText("Click Me"));
Also, the way you have written is not correct. This will fail in case more element are added in between like a new div or a new button. Try avoiding this.
Yes for the current scenario, your way of making wait is right. But for other use case it might not be right to make your application wait explicitly.

Can't click element inside iframe

On the screenhsot below is HTML-code of iframe.
The first two red marked object can be identified by Webdriver however the last marked (which is a button) can't be clicked by Webdriver. I have tried click it using different ways (like click by id, name, etc). But I still can't click the submit button.
Please help me to click that submit button inside the frame.
You need to use switchTo().frame() to access content within a frame or iframe.
driver.switchTo().frame("name"); // where name is the name of the iframe i.e. name="frameName", you would use framename
driver.switchTo().frame(0); // You can switch to the frame by index
driver.switchTo().frame(element); // You can switch to the frame by a WebElement reference
In your particular case you can use:
driver.switchTo().frame("InstantSgn");
To switch out of the frame after you're done within the iframe context:
driver.switchTo().defaultContent();
Same issue happened to me today. It might be simply due to you not switching to the frame, or it could be something I experienced.
This happened to me in IE only. In Chrome there is no problem at all.
Actual Code
As you can see, I did switch to the frame to make sure this element could be found. But, it could not be. The only solution that I found was to put Thread.Sleep(2000) to make it pass. I am not quite sure why, but I guess it has something to do with the content not being available in the DOM.