Identifying elements in Iframe after content change using Selenium - c#

The Problem
I have an iframe which Contains a form , once I submit the form the content of the iframe changes to something else based on the information . So I need to verify the elements/texts in result as a part of my test
What I did
I used Switch().Frame to submit the data to iframe , and used " WebDriverWait " to wait for results (I am waiting for an element )
The issue
After submission new elements do not get recognized and "wait for element" fails after time out even though the result screen visually loaded to the iframe ( I also tried increasing the time out , but no luck )
Am i doing something wrong ? or Can anyone give me suggestion ?
highly appreciate any help

I am working on the same scenario and this is working for it, you can try :
driver.switchTo().frame(frameid);
driver.findElement(By.cssSelector("input[name=\"login\"]")).click();//whatever you want to perform in frame.
driver.switchTo().defaultContent();
Thread.sleep(3000);
driver.switchTo().frame(frameid);
driver.findElement(By.cssSelector("input[name=\"login\"]")).getText();//Whar ever you want to perform.
If you have any concern so let me know.
Enjoy!

I think after submission - the element get changed now the element do not lie in iframe.
Use:
driver.switchTo().defaultContent();
And try recognizing element.
I am not pretty much sure. but can you give it a try.

Is the complete form loading again or just some text/info that you are providing?

Related

C# Selenium: Click button under iframe

I have a iframe modal under my site. I am trying to click button in
it but I am unable to do so. Below is my code. Please let me know
what am i missing
driver.SwitchTo().Frame(driver.FindElement(By.Id("iframeid='frame_name'")));
driver.FindElement(By.Id("sendReuqest")).Click();
Expected Result: Button id: sendRequest should get clicked which is on iframe
Actual Result: Element is not found.
Please let me know if you have any questions.
Try to do it this way. Let's take frame_name id as iframe_1. Whatever you frame id is you can add instead of iframe_1. Also you have a spelling mistake (typo) it might be sendRequest so I am adding as id of your button.
driver.SwitchTo().Frame(driver.FindElement("iframe_1")));
driver.FindElement(By.Id("sendRequest")).Click();
Hope it works. Please do comment and let us know.
Best of luck.
It looks like you're trying to use By.Id() when you should be using By.CssSelector(). By.Id() expects you to pass a parameter which matches the ID element in the HTML.
driver.SwitchTo().Frame(driver.FindElement(By.CssSelector("[iframeid='frame_name']")));
driver.FindElement(By.Id("sendReuqest")).Click();
Try that one:
driver.SwitchTo().Frame("frame_name");
driver.FindElement(By.Id("sendReuqest")).Click();

WebDriver - element is not clickable Chrome

I have following problem. I run test on Firefox and Chrome. On Firefox test run correctly but on Chrome SauceLabs give a message:
unknown error: Element is not clickable at point (717, 657). Other
element would receive the click: <div class="col-md-9 col-sm-12"
style="margin-top:8px;">...</div> (Session info: chrome=36.0.1985.125)
(Driver info: chromedriver=2.10.267521,platform=Windows NT 6.3 x86_64)
I choose element by unique css selector in both test in the same way:
driver.FindElement(By.CssSelector("button.btn-xs:nth-child(1)")).Click();
Any ideas what is wrong here?
I am assuming that you have the correct element you need, ie the XPath is correct.
Here are few ways out:
Try to Click on the parent element instead.
Try .Submit() instead of .Click()
Try to execute the JavaScript that will be executed on the OnClick event of the element you are trying to click.
I have used the 3rd way with success all the time.
Another one
Do a .SendKeys(Keys.Enter) on that element (or a Space key)
Since you've tagged the question as Google-Chrome too - I suppose that this is happening mostly with ChromeDriver. I had the same issues with one of my previous projects (Asp .Net MVC). I found that when some elements are not visible for this Driver if they are not in the screen_visible_area. Please note that they are loaded (HTML, CSS3, JS etc.) properly.
So after a lot of reading and testing, I found that my workaround is simply scroll to the WebElement - so it is in the visible part of the screen. Actually this issue was not for all elements and I didn't find better solution for it.
unknown error: Element is not clickable at point (..., ...)
Is not descriptive error for this case, because like you I also thought that is Selector-related.
Just to be full answer - I had the same problems with IEDriver too. My implementation was to use the Browser scroll down/up options and just "send the screen" where the problematic element is.
Simple JSExecutor code that you can use:
WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(110,350)", "");
or
jse.executeScript("scroll(0, 250);");
or
driver.executeScript("window.scrollBy(110,350)", "");
Other topic-related useful resources are here.
Update
When it comes to the .sendKeys() I also used the browser accessibility features. All you need to do is just count how many TAB clicks your test need in order to get to the targeted web_element. Then just call .click().
Try this simple code:
element.sendKeys(Keys.TAB);
or
element.sendKeys("\t")
or
Actions builder = new Actions(driver);
builder.keyDown(Keys.TAB).perform()
I realize this is a super old question, but it came up while searching a nearly identical problem in the present day. After attempting many of the fixes described here and getting new exceptions for my trouble (mostly stale element and http request timeouts) I stumbled across this issue on Selenium's GitHub.
As described in the post, Chrome had advanced beyond the abilities of my version of chromedriver.exe--my v2.30 driver had known issues with clicking elements due to changes in Chrome v61 scrolling mechanics. Updating to the latest chromedriver.exe solved all my problems.
tl/dr: ensure your version of chromedriver is compatible with the version of Chrome being tested.
I was getting issue that login button is not clickable in chrome even xpath was correct. after browsing many sites, i came to the solution - use .submit() instead of .click() and it worked perfectly.
driver.findElement(By.xpath("//button[#id='loginBtn']")).click();
driver.findElement(By.xpath("//button[#id='loginBtn']")).submit();
If you're doing anything complicated in your CSS, Chrome can get confused (this has happened to me) and think that the element you're trying to click is covered by another element even though this is not the case.
One way to resolve this is to help Chrome understand the situation correctly by adding z-indexes (you'll need to add relative or absolute positioning also) to unambiguously place the correct element on top of the other.
For me, it was creating this command instead.
driver.FindElementByXPath("id('gender1')").SendKeys(Keys.Space);
For my case, I was interacting with radio control.
I have had this problem on FF. This issue happens when your field is not in the view area. The slick way to resolve this issue is to zoom out your browser:
TheNotClickableField.SendKeys(Keys.Control + "-" + "-");
you might want to zoom out more or less according to your page size.
I.
If the button is on the bottom of the page, the following code could be used to get you to the bottom via JavaScript from where the click can be executed:
(driver as IJavaScriptExecutor).ExecuteJavaScript("window.scrollTo(0,document.body.scrollHeight - 150)");
The same action could be done via C# with the Actions class provided by Selenium.
This will get you to the bottom of the page----->
new Actions(Driver).SendKeys(Keys.End).Perform();
Keys.End could be switched with Keys.PageDown // Keys.Space
II.
If you want to get to the exact position of the element you can:
1.Get the element's Y location---> var elementToClick = driver.findElement(By.{Anything}(""));
2.Execute the following JS---> (driver as IJavaScriptExecutor).ExecuteScript(string.Format("window.scrollTo(0,{0})", elementToClickYLocation.Location.Y));
3.Click the element---> elementToClick.click();

cant reach to the text field with Watin

im trying to make an automation with Watin and i'm having an issue reaching to a text fill in an HTML body..
when i log into the site i manage to reach the search box and to put input there and even press "enter" when it moves to the second page, i cant reach the input form there
This is my code - first step is working smooth but second isnt.
browser.GoTo("mywebsiteaddress");
browser.TextField(Find.ByName("sysparm_search")).TypeText(ticketNumber.Text);
System.Windows.Forms.SendKeys.SendWait("{ENTER}");
//browser.TextField(Find.ByName("sys_display.sc_task.u_category")).TypeText(ticketNumber.Text);
browser.Element("sc_task.work_notes");
This is the Browser source code when i check it with google chrome
<textarea wrap="soft" onkeypress="" onkeyup="multiModified(this);fieldTyped(this);" onfocus="this.isFocused=true;" autocomplete="off" data-charlimit="false" rows="16" id="sc_task.work_notes" data-length="4000" style="; width:100%; overflow:auto; " name="sc_task.work_notes" onblur="this.isFocused=false;" onchange="this.isFocused=false;multiModified(this)" onkeydown="multiKeyDown(this);;"></textarea>
Thanks all!
It could be a number of things including what you're looking for is in a frame or less likely you're looking for it before it is loaded. If in a frame, you'll need to specify which frame. If it is not loaded yet the easy way to check is by putting in a generic sleep() call, though that is not the best long term.
When I deal with something I can't find, I make heavy use of the Flash() method. In your case, you'd probably want to start at the whole page level and work your way down to your object. Flash() will show you where you're looking at to make sure you're looking in the right spot on the page, ideally getting down to the parent element of what you're looking for and being able to correctly identify and flash that and then figure out what is amiss with trying to get the textarea you're really trying to get at.
Use Fire fox. Install an add on by the name Firebug.
Whichever element that you want to inspect, you right click on it and say inspect element with fire bug.
once you know the id or name of that element, you can easily access it using the Find class.
Sometimes it so happens that the element is in a different frame than the main frame. watin cannot directly access that element if it is not accessed via the frame.

Try to get for WatiN for links/browsers

I've recently started to test and use WatiN, and i came across some of problems/errors that i cant really solve.
First ill describe what i'm doing with my tests ..
I'll go and get all the links in a specific div in a list ... so i have a link lists ... which im gonna loop thru ...
In the loop im gonna use a link.clicknowait() function, to open that link (it opens in a new ie tab) .. then i sleep thread for like few seconds and i close that browser by attaching that link (url) to a new browser and after that the browser is closed as im using it always with 'using (..)' statement.
So the first problem is that when the browser gets all the links and start to click them one of the links might lead not the right page, meaning like it can lead to a page that is saying page doenst exist anymore, so after that i cant close that page anymore ... how to solve this and attack ie to that not existing page?
It hasnt got fixed url ... anything that you can recommend?
Is there a method something like "Try attach to ...." because else i get an error if it tries to attack a browser to a link which doesnt exist.
Also is there anyway to check if the link in a links list is still the right one, cuz i also had an exeption few times that it couldnt click on that link because it was empty in the page ...
Or can i check something like link.trytoclick() and if an error and just ignores that link and goes further ???
And the last question after clicking alot of links and opening and closing browser i got an error outofmemoryexception .. how can it be, i've used 'using statement' which always closed browser when it was out of it ...
Thanks in advance for the help.
You can use the href to open the link in the same window of your watin session, after you verify the link you can go back to original page.
Some thing like this:
string href = browser.Link("link_id").GetAttributeValue("href"); //or your link from the collection
browser.GoTo(href);
//Perform you check
browser.Back();
To check if the link exists use:
session.browser.Back();

webBrowser control cannot find htmlElement after Ajax webpage update or in frame

Using webBrowser control in a winForm. but when the webpage is updated by Ajax or in a frame, I cannot use
webBrowser1.document.getElementById, etc. to find that htmlElement. The element also won't show in the View->Source code in IE.
The untimate purpose is to find that htmlElement and simulate a click or other function like
invokeMember("staff").
The WebBrowser's Document object does indeed represent a live view of the DOM so there may be some other reason that you're unable to find it. DOM updates will not however be represented in View -> Source. You should use IE8's developer tools which will show you a live view of the DOM and maybe you'll see something like an incorrect/duplicate ID or something.
I'm guessing you have already solved this problem on your own, but if you haven't, refer to my question here: WebBrowser Control and GetElement by ID
Essentially, if you do something to the WebBrowser control (ie, add some member to the DOM) it will do so asynchronously. That is, it does it on another thread, that way it avoids locking your calling thread when the WebBrowser is doing work. The problem is that if you programatically modify something with a command, you will have to wait till that command actually finishes loading its changes till you can work with the result of it.
Check my question there for a code example of what I was doing. I hope someone can find my previous trials useful.

Categories

Resources