How to send a single quote to an input element with Selenium? - c#

When i send a single quote with the SendKeys() method, Selenium sends two single quotes instead. I am using C# with the Chrome Driver.
Here is my code:
IWebDriver driver = new ChromeDriver();
var element = driver.FindElement(By.Id("uid"));
element.Clear();
element.SendKeys("admin'--");
The textbox on the tested web page receives the following value:
admin''--
How to send a single quote to an element?

the code sample you sent works for me.
I tried with fireFoxDriver:
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://www.google.com");
var element = driver.FindElement(By.Name("q"));
element.Clear();
element.SendKeys("one quote: ', double quote \"");
and the output in google search box is:
one quote: ', double quote "
try to start with the simplest example, and insert one quote by: "'"

try escaping string with \, also remember to use verbatim # symbol
element.SendKeys(#"admin\'--\'");

I also think it is related to ChromeDriver as it worked on Firefox. Anyways I have used the JavaScript for this and it worked.
WebElement element = driver.findElement(By.id("id"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].value="test'w";", element);

Related

How to open 2 different links in two tabs using a singe Selenium c# Webdriver/ChromeDriver?

Following is my code
IWebDriver driver = new ChromeDriver();
driver.Url = "https://www.google.com/";
driver.Url = "https://login.yahoo.com/";
I want that both the links should be opened in different tabs of same browser window
How to achieve this?
TIA
Try this:
((IJavaScriptExecutor)driver).ExecuteScript("window.open();");
instead of
IWebElement body = driver.FindElement(By.TagName("body"));
body.SendKeys(Keys.Control + "t");
If you need to continue working on the first window you need to follow steps described here just remember to change Ctrl + t with the JavaScript above.
You should be able to do that as follows:
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL+"t");
driver.get("https://login.yahoo.com/");
Good luck
Andreas

UriFormatException shown in console for unity build.How to solve this?

I am running the below code
string URLL = Alldetails[0].Urls[0];
Debug.Log("URL link is = " + URLL);
WWW imgurllinks = new WWW(URLL);
My console Output is shown below
URL link is = "http://testing.com/test/storage//productphotos/HYTh3zUYjQKuHavNSpjQ1xeUq7laeS1WwOKPOkpQ.jpeg"
UriFormatException: URI scheme must start with a letter and must consist of one of alphabet, digits, '+', '-' or '.' character.
But if I give that same link directly and executes code
WWW imgurllinks = new WWW("http://testing.com/test/storage//productphotos/HYTh3zUYjQKuHavNSpjQ1xeUq7laeS1WwOKPOkpQ.jpeg")
From the API I tried by changing the link to "http://testing.com/test/storage/productphotos/HYTh3zUYjQKuHavNSpjQ1xeUq7laeS1WwOKPOkpQ.jpeg" that is removing one slash after storage in the link.
The same error but if I run the link directly than saving in a string it will work.Doesnt make any sense.

C# selenium load html

I want to load html from WebClient() to selenium driver.
I have:
WebClient glavniklijent = new WebClient();
string HTML = glavniklijent.DownloadString("http://www.bodum.com/gb/en-us/shop/detail/10948-01/");
If I save it like local html file and then navigate on it
driver.Navigate().GoToUrl(localfile);
It wont help because then it will request online resources. Which take too long.
Also I tried with Javascript Executor
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
string title = (string)js.ExecuteScript("document.write('" + HTML +"')");
But that don't work.
Reason why I do this is For me easiest way for parsing html is with Selenum driver, I tried with HtmlAgilityPack but I never used it before and it seems much complicated compared with Selenium Select By Id, Select by classname etc.
Can I load this with selenium locally ?
Is there html parser similar to selenium ?
Try CsQuery
https://github.com/jamietre/CsQuery
https://www.nuget.org/packages/CsQuery/
It makes parsing HTML pretty easy, in a way very similar to jQuery:
var document = CsQuery.CQ.CreateDocument(html);
foreach (var element in document.Select("ul.somelist > li.thread"))
{
// do something with element
}

Accessing JQuery Auto complete with Selenium Webdriver

I am trying to get the HTML of a JQuery autocomplete after a word is typed in. To do this I decided to try selenium webdriver.
I had a read of the documentation and I think I got couple of issue.
1) I'ts not finding the search suggestions although I am pretty sure I am selecting it right
2) I don't know how to get the HTML to a string from an IWebElement.
Here is my code that doesn't work:
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("https://site.xxx/");
IWebElement query = driver.FindElement(By.Name("stext"));
query.SendKeys("iphone");
// Everything up to this point works, I put a wait in here just incase it takes a few seconds to load
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(2));
// This seems to come back empty but in developer tools for chrome I can see it
IWebElement results = driver.FindElement(By.ClassName("ac_results"));
Can anyone see where I am going wrong?
On your result call add .text(); to end to get the text value of the element.
Java syntax.
String val = driver.findElement(By.ClassName("ac_results")).text();
Or
WebElement we driver.findElement(By.ClassName("ac_results"));
String val = we.text();
I'd try the javascriptexecutor, see below: (C# version)
IWebDriver driver;
IJavaScriptExecutor jse = driver as IJavaScriptExecutor;
string body = (string)jse.ExecuteScript("return document.body");

Getting DOCTYPE using selenium webdriver C#

I am using Selenium webdriver for UI automation purpose. Below is my sample code
IWebDriver driver = new OpenQA.Selenium.IE.InternetExplorerDriver();
string url ="http://stackoverflow.com";
driver.Navigate().GoToUrl(url);
string pagesource = driver.PageSource;
pagesource variable does not have the doctype. I need to know the DOCTYPE for W3C validation. Is there any way to get DOCTYPE of html source through selenium?
This thread shows there is no way to get the Doctype of html source through selenium, instead you can do a HTTP request from .net and get the DOCTYPE. I don't want to do a seperate HTTP request for getting DOCTYPE.
Using FirefoxDriver instead of InternetExplorerDriver will get you the DOCTYPE. Unfortunately this won't solve your problem - the source you're getting with driver.PageSource is already preprocessed by the browser, so trying to validate that code won't give reliable results.
Unfortunately there are no easy solutions.
If your page is not password protected you can use "validate by uri" method.
Otherwise you need to obtain page source. I know two ways of doing it (I implemented both in my project). One is to use proxy. If you are using C# take a look at FiddlerCore. Other way would be to make another request using javascript and XMLHttpRequest. You can find example here (search the page for XMLHttpRequest).
For W3C validation basically we have 3 issues if we automate through selenium webdriver.
Getting proper page source since driver.Pagesource is not reliable.
Getting doctype of HTML source.
Dealing with controls rendered through ajax calls. Since we cannot access these controls in page source how do we get the exact 'Generated source' of the page?
All the above things can be done by executing javascript through selenium web driver.
in a text file called 'htmlsource.txt' store this below code snippet.
function outerHTML(node){
// if IE, Chrome take the internal method otherwise build one as lower versions of firefox
//does not support element.outerHTML property
return node.outerHTML || (
function(n){
var div = document.createElement('div'), h;
div.appendChild( n.cloneNode(true) );
h = div.innerHTML;
div = null;
return h;
})(node);
}
var outerhtml = outerHTML(document.getElementsByTagName('html')[0]);
var node = document.doctype;
var doctypestring="";
if(node)
{
// IE8 and below does not have document.doctype and you will get null if you access it.
doctypestring = "<!DOCTYPE "
+ node.name
+ (node.publicId ? ' PUBLIC "' + node.publicId + '"' : '')
+ (!node.publicId && node.systemId ? ' SYSTEM' : '')
+ (node.systemId ? ' "' + node.systemId + '"' : '')
+ '>';
}
else
{
// for IE8 and below you can access doctype like this
doctypestring = document.all[0].text;
}
return doctypestring +outerhtml ;
And now the C# code to access the complete AJAX rendered HTML source with doctype
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
string jsToexecute =File.ReadAlltext("htmlsource.txt");
string completeHTMLGeneratedSourceWithDoctype = (string)js.ExecuteScript(jsToexecute);

Categories

Resources