Get text element from a javascript alert using webdriver - c#

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.

Related

Get to Data in Clipboard from Selenium Client

I want to access some data which is copied to the clipboard on a website when you click a button (Jscript)
I use Remotewebdriver (ChromeDriver) to control the testcase but couldn't find out how to access this information.
Anybody knows how?
If possible with c# but a java solution may also work.
Thx
So I could not figure out how to do this so my way was to render a textarea with Jquery and CTRL+V the text into that box just to then get the value back with webdriver. Not pretty but it works
var javaScript = "$( \".d-modal-footer\").html('<div><textarea id=errormessagecopy maxlength=\"5000\" cols=\"80\" rows=\"40\"></textarea></div>');";
WebDriver.ExecuteJavaScript(javaScript);
var errormessagecopy = WebDriver.FindElement(By.Id("errormessagecopy"));
new Actions(WebDriver).MoveToElement(errormessagecopy).Click().KeyDown(OpenQA.Selenium.Keys.Control).SendKeys("v").KeyUp(OpenQA.Selenium.Keys.Control).Perform();
var errorText = WebDriver.ExecuteJavaScript<string>("return $(\"#errormessagecopy\").val();");
You can use https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API but as described in the documentation to read the value from it you need to grant permission in the browser. After you have permission simply execute js script on-page from selenium and get that value.

Select option set values in Selenium Webdriver

I'm trying to automate the drop down field value using c# selenium. I use the following code snippet for select the drop down value and as well as the option.
Code
But I'm not able to click that option .If I trying to write a click functionality for the selected option I'm getting error.
Cannot click on option element.Executing javascript function returned an un expected error,but no error could be returned from IE's
javascript Enginge. .
Please refer the following Screen shot for the Error reference .
Error reference
How can I change the value in dropdown using selenium c# ?
I'm not familiar with C# but this is how you'd do it in java. I believe the syntax is similar so maybe someone can translate it or something?
List<WebElement> dropdown = driver.findElements(By.cssSelector("Your css/xpath/whatever here");
for (int i =0;i<dropdown.size();i++){
WebElement ele = dropdown.get(i);
String textOfElementYouWantToClick = ele.getText();
if(textOfElementYouWantToClick.contains("Value you are looking for")){
ele.click();
System.out.println("Selecting the value you chose");
}
}
Try using the SelectElement class. It has methods for handling dropdown fields.
https://seleniumhq.github.io/selenium/docs/api/dotnet/html/T_OpenQA_Selenium_Support_UI_SelectElement.htm
Have you tried not using javascript to click selected option?
There is Select selenium class to handle dropdowns:
Select myDropdown = new Select(dropdownWebElement);
myDropdown.selectByVisibleText('Value to select');
The other reason for it might be the dropdown is rendered with some framework that keeps actual Select hidden and what you see on screen is actually items from very different element in your DOM.

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

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

Retrieve Selected Text from a Web Browser Control in a

Here's what I am trying to do:
Select text from a webpage I pulled up using my web browser control.After clicking a button while this text is still selected I would like a message box to pop-up displaying the text that was highlighted by the user. How do I get this functionality to work in my wpf application?
I think I'm on the right track using mshtml but I get an error that says:
Error HRESULT E_FAIL has been returned from a call to a COM component.
This error will happen even when I try something small on the document like changing the title.
The code is below:
IHTMLDocument2 doc = (IHTMLDocument2)this.webBookText.Document;
doc.title = "l";
Well, for starters it would be a lot simpler to use WebBrowser than mshtml (note that you can still host WebBrowser in WPF) - this will certainly let you do simple things a lot easier:
webBook.Document.Title = "foo";
However, I can't see anything there that would let you work with selections very easily...
You can get the selected element with .Document.ActiveElement, but this is the entire element - not the selected portion.
Figured it out that error was because this wasn't in my form class

Categories

Resources