C# Selenium Web Driver Exception after using SendKeys Function - c#

Hi I am having problems with my web driver after using the SendKeys function.
What I am trying to do is after clicking an OK button on a dialogue box after clicking a 'Save' button on a webpage I am then trying to get the newly opened page's url.
My web driver is functioning correctly before using the SendKeys function, as I am able to click buttons, get urls etc.
I thought the issue might have been to do with the web page losing focus after clicking the OK button on the dialogue box so I used the following code (but this did not work either):-
> System.Collections.ObjectModel.ReadOnlyCollection<string> winHandle2 =
> _webDriver.WindowHandles;
>
> _webDriver.SwitchTo().Window(winHandle[0]).SwitchTo();
The exception I am getting can be seen below:-
An exception of type 'System.InvalidOperationException' occurred in WebDriver.dll but was not handled in user code
Additional information: [JavaScript Error: "a is null" {file: "file:///C:/Users/andrew.short/AppData/Local/Temp/anonymous.3779fc41a91f475c89d01937ed7bb71b.webdriver-profile/extensions/fxdriver#googlecode.com/components/command_processor.js" line: 8166}]'[JavaScript Error: "a is null" {file: "file:///C:/Users/andrew.short/AppData/Local/Temp/anonymous.3779fc41a91f475c89d01937ed7bb71b.webdriver-profile/extensions/fxdriver#googlecode.com/components/command_processor.js" line: 8166}]' when calling method: [nsICommandProcessor::execute]
If there is a handler for this exception, the program may be safely continued.
I appreciate any help anybody can give me :-)

public static void SendKeys(this IWebElement element, string value, bool clearFirst)
{
if (clearFirst) element.Clear();
element.SendKeys(value);
}
This function is very simple, all it does is make our code a little from concise. In the original Selenium RC, SendKeys would clear a text field and then send the required text. In Selenium 2, text fields are not cleared first unless explicitly executed. We can then combine it to allow something like:
myElement.SendKeys("populate field", clearFirst: true);

Related

Click Dynamic button with Selenium (Pinterest/TailwindApp)

I am trying to automate the scheduling of pinning items to Pinterest using Tailwindapp.com. I am using a console app in .NET (C#) with Selenium Chromedriver. I start up the browser and enable the tailwind extension and login to tailwind. Then I go to the site I am trying to pin images from, get to the product page, search for the button and attempt to click it. That's where it falls apart. The 'Schedule' button in Tailwind appears over all images on the page as you hover. When I do an XPath search, it only returns a single button for the whole page (the console line below shows 1).
public static void ClickScheduleButton(IWebDriver driver) {
// get all the buttons and then use the first one
IList<IWebElement> buttons = driver.FindElements(By.XPath("//*[#id='tw_schedule_btn']"));
Console.WriteLine("Number of items found: " + buttons.Count());
IWebElement scheduleButton = buttons.ElementAt(0);
Actions actions = new Actions(driver);
actions.MoveToElement(scheduleButton).Click().Perform();
}
On the perform method, I get the following error: OpenQA.Selenium.WebDriverException: 'javascript error: Failed to execute 'elementsFromPoint' on 'Document': The provided double value is non-finite.
From what I've read, this seems to mean that there are more than 1 element but I seem to have ruled this out with the number of buttons found. I have attempted to do a wait to make sure it's available but I do not believe that is the issue.
I have tried to find an example just trying to do this with a Pinterest button because in theory it would be the same logic but I cannot find anything for that either.
My assumption is that it's a problem just getting the button to appear on the correct image? But that's just a guess.
If I'm understanding this correctly you want to click the button who appears in when you hoover the mouse in the image.
If you can hoover the mouse and wait until the expected xpath appears should solve it.
protected virtual void HooverMouse(By element)
{
Actions action = new Actions(driver);
action.MoveToElement(driver.FindElement(element)).Build().Perform();
}
The element will be the first image you have, so you must change your driver.findelements("the button") to the "images" and loop arround this.
In pseudo code will be:
Hoover(image) -> WaitButtonAppears -> ClickButton

Click event not working for Firefox but is for chrome

The click event is not firing in firefox but works ok in chrome.
The test fails with the error: "Element not found on page."
Below is the code and HTML for the button I want to click.
Browser.ElementClickById("ctl00_ContentPlaceHolderBody_lvProducts_ctrl0_ctrl1_btnAddProductToCart_input");
and inside the elementclickbyid i have:
driver.FindElement(By.Id(elementID)).Click();
HTML code is:
event
You could try working around with a Javascript click.
// declare JS executor
var executor = (IJavaScriptExecutor)Driver;
// locate the input
var input = Driver.FindElement(By.XPath("//input[#type='submit']"));
// execute JS to click
executor.ExecuteScript("arguments[0].click();", input);
I've seen cases where regular Click(); does not work across browsers -- these cases are rare, but using JS click usually works across multiple browsers when I run into this issue.
driver.findElement(By.xpath("//input[#type='submit']")).click();
i am sure you are trying to use browser class to keep your methods there, but try to use xpath not id. just use this code to click what you need. don't use page object model or anything else. don't save it in your browser class under click method. just in your main code use this code to click. and before to run it make sure that you have only one type submit. if its gonna show to you 2 types then use this code
driver.findElement(By.xpath("//input[#type='submit'][1]")).click();
number 1 says click to first submit if the button which you need second then follow the logic and change the number to 2
driver.findElement(By.xpath("//input[#type='submit'][2]")).click();
for better answer share your code class and also URL where you are trying to click button and also which element you are trying to click

Confirming asp c# action on page load

I've been searching for an answer to this but haven't found what I'm looking for yet.
When I load into the page there is a check run one the server side. Depending on the output of this (bool), I wish to display a "yes no" confirm box to execute another piece of server side code.
I have found ways to do this easily enough on a button click but I'm trying to avoid adding a hidden button and simulating a click.
MessageBox.Show isn't an option in this case as I get the following error:
Showing a modal dialog box or form when the application is not running
in UserInteractive mode is not a valid operation
Is there any way to achieve this without simulating a button click?
Cheers,
Spitfire2k6
In web app (including the one created with ASP.NET) you can use Javascript confirmation dialog box: window.confirm("Request to Confirm Text");, and process User's response like in the following sample code snippet:
var _response = confirm("Please Confirm");
if (_response == true) {//Do Action1}
else {//Do Action1}
Pertinent to your case, you can use for e.g. page <body onload> event. Hope this may help.
I realised I was going about this all wrong.
The check is now done on page load, depending on the result I'm showing an asp panel with 2 asp buttons as a confirm box.
Thanks for all guidance.
Cheers,
Spitfire2k6

Selenium Webdriver: can't submit text into textbox area

I've been stuck trying to do some automating tests with AOL mail service (sending text messages). I want to insert text into a textbox and submit it but it fails all the time. In AOL web interface, I have to type the message, then hit "Enter" to submit or send it.
Here's how the textbox looks like:
<div id="dijit__Widget_66" class="inputContainer" layoutalign="bottom" data-dojo-type="ws/widget/Pane" widgetid="dijit__Widget_66">
<textarea class="wsInput" tabindex="201" data-dojo-attach-event="onkeyup: onKeyUp, onkeypress: onKeyPress, onblur: onBlurTextarea" data-dojo-attach-point="messageInput" style="width: 316px;"></textarea></div>
My code of selecting and inserting text into textbox area works fine:
IWebElement ele = driver.FindElement(By.CssSelector("div[id*='dijit__Widget'].inputContainer>textarea.wsInput"));
ele.Clear();
ele.SendKeys("Hello");
But thing turns to be complex when I want to submit the text, neither of these work:
ele.Submit();
ele.SendKeys(Keys.Enter);
SendKeys(Keys.Enter) only adds a new line into the textbox instead of submitting and sending the message.
I'm desperately looking for help!
**I've discovered that the command Keypress - Value: "013" (Enter key) in Selenium IDE works. But it only works with Selenium IDE, when exporting to Webdriver, I receive this error:
// ERROR: Caught exception [ERROR: Unsupported command [keyPress | css=div[id*='dijit__Widget'].inputContainer>textarea.wsInput | 013]]
Way too late for this post but for anyone else looking to solve this. Check and see if there is a function or event that is triggered when you submit text to that specific field. If there is, see if you can remove it by executing a script on the IDE.
It will look something like the image below:
Here's how you can remove events using JS:
document.getElementsByTagName('input')[input_index].attributes.removeNamedItem("eventname");
Have you tried using TAB, not sure if this will work but you can give it a try :
ele.SendKeys(Keys.Tab);

Get text element from a javascript alert using webdriver

I'm using selenium webdrive in c# for learning and writing automated tests and i have come across this situation:
Say you have a website like this: http://referencewebapp.qaautomation.net/register.php and you get the pop-up with the error message as a javascript alert(if you press Register without filling in any info, for example). What i need to do is to check what error message (the text) is in the alert.
I cannot use driver.FindElement or drive.PageSource as i cannot see the source code for it. I read about trying to select/change the frame(Selenium cannot get alert thrown in iframe), but i don't know how the frameID. Is there any way i can find and check that text?
Thanks for any suggestions.
driver.SwitchTo().Alert().Text;
should be all you need.
You can try to get the text of an alert (dialog) by using selenium Alert object.
for example:
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (alertText.equals("error...")) {
// Do Something...
// dismiss alert
alert.dismiss();
}
More information in the selenium 2.0 documentation.

Categories

Resources