Can't click element inside iframe - selenium

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.

Related

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.

How to handle (X) button inside iframe using 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.

Clicking label element that unfortunately contains a link

I'm running into an issue where I'm attempting to click on a checkbox. The app is written in AngularJS.
The checkbox can not be clicked, as technically the element is not visible. Using 'visible: false' does not help to get this box checked. I have also tried to use element.set(true)
The checkbox is within a label element that happens to also contain hyperlinks. Since Capybara/Selenium clicks on the middle of an element by default, I end up opening the hyperlink instead of marking the checkbox. Clicking within anywhere in the label (outside of the hyperlinks) checks the box successfully.
How can I achieve this?
I would recommend using JavascriptExecutor to click element. In case element is technically not visible Webdriver will not allow you to click it using API, as it tries to simulate real user actions. Nevertheless you have the option to execute JS directly, which doesn't really care.
This answer will help you with the code part:
https://stackoverflow.com/a/19934852/2998104
You would need to change ending slightly to:
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement element = driver.findElement(By.xpath("exact_locator_of_checkbox"));
js.executeScript("arguments[0].click()",element);
Of course no need to use only xpath, but anything that help you point to the checkbox directly.
Took me a little while but I figured out that I can avoid the Capybara API and use WebDriver's ActionBuilder.
def agree
source = find('label.terms-label').native
actionbuilder = page.driver.browser.action
actionbuilder.move_to(source, 0, 0).click.perform
end
By default, the Capybara API (as well as other native Selenium methods) will click in the center of the element. This will avoid that and will find the element and click it at (0, 0) coordinates.
As of Capybara 3.0.0, you can specify an offset when you call click on an element. So if you want to click the top-left corner of the element box, you'd say:
find('label.terms-label').click(x: 0, y: 0)
(Note, you must specify :x and :y if you specify either)
API Docs
Relevant commit

How to click on Toolbar Item with selenium?

Web page contain a button with some text for example "Test". This button actually is a toolbar element. ( class ="tbButton" id="id",text="Test") and redirects to a certain table when press on it.
When try to use the following click methods
selenium.click("id");
selenium.doubleClick("id");
selenium.click("//*[text()='Test'and contains(#class, 'tbButton')] ");
the button does not react
Could enybody show an alternative methods that is able to resolve a problem
It's hard to know exactly what the problem is without knowing more about the actual contents of the page you are testing. Is there an example of the toolbar online somewhere?
With modern interfaces, locating elements with Selenium is not always an exact science. Here are a few suggestions:
With modern interfaces you often find that the DOM is being manipulated, so it is possible that the identifier you are using is no longer valid by the time you get to your click(). Use Firebug to check that you have the correct element.
Often it helps to click on the parent of the element, such as a div or the parent table cell. Again, use FireBug, to try some other elements near your toolbar button. Alternatively, Firebug sometimes reveals that the element contains other elements. You might have more luck changing the target to a contained element instead.
Sometimes you have to play around with some of the alternative actions. For instance, some controls respond to a mouseDown() followed by a mouseUp(), but not to a click(). Again you can often get hints from looking at the source with Firebug.