Selenium Webdriver: can't submit text into textbox area - c#

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

Related

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

*Problems finding specific element on page

I know there are existing questions on this topic, but none of them seems to help me with this:
I've got a lightbox with several elements.
I can find and access all of these elements, except ONE, using the XPath.
These are the items:
Text header: No problem
Text: No problem
Input field: No problem
Text: No problem
Text: No problem
Button (upload file): THIS IS SEEMINGLY IMPOSSIBLE FOR Selenium TO FIND
Button (cancel): No problem
Button (send): No problem
The XPaths for all the elements:
.//*[#id='overlays']/overlay--master/div/div/overlay-lightbox/div/div[1] /content-placeholder/h1
.//*[#id='overlays']/overlay--master/div/div/overlay-lightbox/div/div[3]/content-placeholder/ul[1]/li[1]/label/span
.//*[#id='overlays']/overlay--master/div/div/overlay-lightbox/div/div[3]/content-placeholder/ul[1]/li[1]/div/div/input
.//*[#id='overlays']/overlay--master/div/div/overlay-lightbox/div/div[3]/content-placeholder/label
.//*[#id='overlays']/overlay--master/div/div/overlay-lightbox/div/div[3]/content-placeholder/span
.//*[#id='overlays']/overlay--master/div/div/overlay-lightbox/div/div[3]/content-placeholder/a/input
.//*[#id='overlays']/overlay--master/div/div/overlay-lightbox/div/div[5]/content-placeholder/button[1]
.//*[#id='overlays']/overlay--master/div/div/overlay-lightbox/div/div[5]/content-placeholder/button[2]
The problematic element is this:
.//*[#id='overlays']/overlay--master/div/div/overlay-lightbox/div/div[3]/content-placeholder/a/input
As far as I can see, there's no reason why it should be different from the other elements (textfield, button, text)?
I'm accessing all these elements with an implicit wait, to check that they've all loaded before continuing.
GCDriver.WaitForVisible("//*[#id='overlays']/overlay--master/div/div/overlay-lightbox/div/div[3]/content-placeholder/a/input");
From the GCDriver (Selenium Driver) class:
public static void WaitForVisible (string xpath) {
var wait = new WebDriverWait(GCDriver.Instance,
TimeSpan.FromSeconds(10));
wait.Until(driver =>
driver.FindElement(By.XPath(xpath)).Displayed);
}
Now, as mentioned, this works for all the other elements, as well as accessing them directly. For this, the wait times out with WebDriverTimeoutEsception:
Result Message:
Test method Tests.Regression_tests.VerifyOverlays.Verify_Update_Ticket_OverlayContent threw exception:
OpenQA.Selenium.WebDriverTimeoutException: Timed out after 10 seconds
Also, of course, trying to ACCESS the button with .Click() also fails:
GCDriver.Instance.FindElement(By.XPath(".//*[#id='overlays']/overlay--master/div/div/overlay-lightbox/div/div[3]/content-placeholder/a/input")).Click();
Result Message:
Test method Tests.Regression_tests.VerifyOverlays.Verify_Update_Ticket_OverlayContent threw exception:
System.InvalidOperationException: unknown error: Element is not clickable at point (-208, 307)
Here's the html code for the element:
<a class="btn btn-grey file-input-container btn-small" data-bind="enable: !uploading() "
style="margin-top: 10px; padding: 7px 12px; "data-tooltipped=""
aria-describedby="tippy-tooltip-32"
data-original-title="Add Attachment">
<i class="fa fa-cloud-upload"/>
<span class="mq-for-small-hide">
<span localize-me="">Add Attachment</span>
</span>
<input data-bind="upload: addAttachments,
enable: !uploading()" type="file"/>
</a>
I've tried some other ways of getting the element, but since this is quite (imo) "messy" html, with no unique ID's or good class names, I've been unable to figure out how.
And it REALLY bugs me that I cannot find it by the XPath. There are 8 elements on the page, all visible and accessible, but this ONE element is impossible to find with Selenium.
The element is there; I can manually click the button on the page while Selenium runs it.
UPDATE:
I also tried using .Enabled instead of .Displayed. Same result.
UPDATE 2:
There are two answers below, and I have to select one as the "winner".
Shubham Jain gives an answer that, while not the exact thing I was trying to to, is a very good work-around. By using JavaScriptExecutor to try clicking the button, it also checks if the button is visible. However, the answer given doesn't do what it tries to do; Clicking doesn't work quite that way. See Solution below to see the correct/working code to click a button using JavaScriptExectutor.
smit9234's answer is exactly what I'm trying to do, although clicking doesn't work that way. To click the button, JS is necessary in this case. However, the question was how to check .Displayed, and that works with the modified XPath he gave me from the code excerpt.
Solution
The XPath of the element (button) is, according to FirePath:
.//*[#id='overlays']/overlay--master/div/div/overlay-lightbox/div/div[3]/content-placeholder/a/input
This, however, doesn't work. Selenium simply cannot find it, even though it's clearly there.
THIS XPath, however, does work:
.//*[#id='overlays']/overlay--master/div/div/overlay-lightbox/div/div[3]/content-placeholder/a/span/span
However, it works with reagards to the .Displayed check. It does NOT work with Click(). To be able to click the button, I began with Shubham Jain's code example and created this method in the Driver class, to be able to use JavaScript (with Selenium's JavaScriptExecutor) to click the button:
using OpenQA.Selenium.Interactions;
public static void JSClick (string xpath) {
IWebElement icon = Instance.FindElement(By.XPath(xpath));
Actions ob = new Actions(Instance);
ob.Click(icon);
IAction action = ob.Build();
action.Perform();
}
Looking at the html snippet you posted, it seems like this is a file attachment function. Based on the html structure of the snippet, try using the following xpath:
.//*[#id='overlays']/overlay--master/div/div/overlay-lightbox/div/div[3]/content-placeholder/a/span/span
You should then be able to use the click(); method to click the "Add Attachments"
I assume that clicking on the input doesn't do anything, however you should be able to use the sendKeys(); method for sending the "file path" to the input element.
Use below XPath :-
//input[#type='file' and contains(#data-bind,'upload: addAttachments')]
You can use javascriptexecutorof selenium to click on button. It operated directly on JS of page.
In java :-
WebElement element = driver.findElement(By.id("gbqfd"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
I don't know more about c# but I believe it something like
IWebElement clicks = driver.FindElement(By.Id("gbqfq"));
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("arguments[0].click();", clicks);
Change the locator in above elements as per your convenience.
Below you will find more details of javascriptexecutor
https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/JavascriptExecutor.html
Hope it will help you :)
May be input element is not visible on the page. you may not use displayed function for that element and try with enabled as given below.
public static void WaitForEnabled (string xpath) {
var wait = new WebDriverWait(GCDriver.Instance,
TimeSpan.FromSeconds(10));
wait.Until(driver =>
driver.FindElement(By.XPath(xpath)).Enabled);
}
if the above is not working, you try to click on anchor tag instead of input.
It seems like it's not clickable. It looks like there's some javascript on the page with a function called "uploading()".
since you're button has this on it
enable: !uploading()
just a test to verify if this is actually the cause, put a breakpoint before your click. on the browser dev tools stick a breakpoint in the uploading() function on the javascript file and see what it's returning.
If this is the case you'll have to use the javascript executor to bypass this.

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

C# Selenium Web Driver Exception after using SendKeys Function

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

Issues with displaying messagebox in asp.net

I have the following message box in c# on my asp.net page inside the btnSubmit_Click event.
It tends to popup sometimes and not popup sometimes. Any reasons as to why it is not consistent?
ClientScript.RegisterStartupScript(
GetType(),
"alert",
"alert('An email has been sent to Customer Service');",
true);
I guess that this will depend on the text you are putting inside the alert. In the example you provided the text is hardcoded but I suppose that in your real application this text is dynamic and might contain characters that break javascript such as '. Try using FireBug to see if there are some javascript errors when it doesn't work.
Have You checked, if the alert('An email has been sent to Customer Service'); line is in the HTML Source after you clicked the Button and the message did NOT appear?
If it isn't in the HTML, check:
with the Debugger if your
codeblock is hit
are you maybe redirecting the response?
try these popups instead
type java directly in the visualstudio GUI
On a button go to the "OnClientClick" property (its not into events*) over there type:
return confirm('are you sure?')
it will put a dialog with cancel ok buttons transparent over current page if cancel is pressed no postback will ocure. However if you want only ok button type:
alert ('i told you so')
The events like onclick work server side they execute your code, while OnClientClick runs in the browser side. the come most close to a basic dialog
as this codes is so small it should work unless they have really strange browser clients

Categories

Resources