Accessing JQuery Auto complete with Selenium Webdriver - c#

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

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

Can title of browser be changed in selenium c#?

In Selenium using Webdriver and C# how we can change the browser title?
Using javascript and jQuery as follow:
document.title='XXX'
or
$('title')[0].text='XXX'
Have no effect although we can change the title using Web developers tool console.
Is there any restriction in changing browser title in Selenium?
UPDATE:
Problem roots: Using JavaScriptExecutor that has been initialized with the driver on a window which had been closed.
As said in this answer,
you can run javascript code from selenium.
Your code will be like this:
WebDriver driver; // assume assigned elsewhere
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
string title = (string)js.ExecuteScript("document.title = 'hello'");
And it will change browser title.
Edit
Here full working code:
ChromeOptions options = new ChromeOptions();
options.AddArguments("--start-maximized");
var driver = new ChromeDriver(options);
driver.Navigate().GoToUrl("http://www.google.com");
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
string title = (string)js.ExecuteScript("document.title = 'hello'");
And here result:

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
}

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

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

Selenium WebDriver and browsers select file dialog

I'm using selenium webdriver, C#.
Is it possible to make work webdriver with Firefox select file dialog?
Or must I use something like AutoIt?
If you are trying to select a file for upload Selenium 2 supports HTML file inputs. For example:
HTML
<input type="file" id="uploadhere" />
Selenium Code
IWebElement element = driver.FindElement(By.Id("uploadhere"));
element.SendKeys("C:\\Some_Folder\\MyFile.txt");
Basically you "type" (with SendKeys) the full file path to the file input element. Selenium handles the file selection dialog for you.
However if you want to manipulate an arbitrary file selection dialog, then like Anders said, you have to go outside of Selenium.
No, WebDriver cannot interact with dialogs - this is because dialogs are the domain of the operating system and not the webpage.
I know people that have had luck with autoit as well as the Automation API provided by .Net.
Another option would be to skip the file dialog entirely and issue a POST or a GET, but this requires more advanced knowledge of the website as well as understanding how construct a POST/GET.
You could try Webinator, it is similar to Selenium in the sense that it is powered by WebDriver. It provides file dialog capabilities and I've had great success with it.
Here is another solution using remotewebdriver, it works like magic and I loved it.
Here is the class I have:
driver.findElementByLinkText("Upload Files").click();
driver.setLogLevel(Level.ALL);
System.out.println(driver.getCurrentUrl());
WebElement element = driver.findElement(By.xpath("//input[#name='file_1']"));
LocalFileDetector detector = new LocalFileDetector();
//Now, give the file path and see the magic :)
String path = "D://test66T.txt";
File f = detector.getLocalFile(path);
((RemoteWebElement)element).setFileDetector(detector);
element.sendKeys(f.getAbsolutePath());
//now click the button to finish
driver.findElementByXPath("//html/body/div[9]/div[1]/a/span").click();
You asked for using AutoIt for the file dialog. This is easy and you can do it with C#.
Install nuget package AutoItX.Net
Use the demo code below
Change the dialog title string as you need
public static void InsertIntoFileDialog(string file, int timeout = 10)
{
int aiDialogHandle = AutoItX.WinWaitActive("Save As", "", timeout); // adjust string as you need
if (aiDialogHandle <= 0)
{
Assert.Fail("Can't find file dialog.");
}
AutoItX.Send(file);
Thread.Sleep(500);
AutoItX.Send("{ENTER}");
Thread.Sleep(500);
}
This helped me after I had trouble with Appium/Selenium related to file dialogs.
According to Nadim Saker
.Net has a library to handle file upload dialog. It has a SendKeys class that has a method SendWait(string keys). It sends the given key on the active application and waits for the message to be processed. It does not return any value.
This can be done as follows, tested and working with Internet Explorer and Chrome driver
var allowsDetection = this.Driver as IAllowsFileDetection;
if (allowsDetection != null)
{
allowsDetection.FileDetector = new LocalFileDetector();
}
Driver.FindElement(By.Id("your-upload-input")).SendKeys(#"C:\PathToYourFile");
Reference https://groups.google.com/forum/#!msg/webdriver/KxmRZ8MkM4M/45CT4ID_WjQJ
If you want to upload a file, and not use the WebDriver, the only solution I've come across is AutoIt. It allows you to write a script and convert it to an executable which you can then call from within your code. I've used it successfully while working with an ActiveX control.
Another approach is to use System.Windows.Forms.SendKeys.SendWait("pathToFile"). I use it with success everywhere where i cant just send keys to element like described by #prestomanifesto.
I used this to solve the problem... try it if all above does not works
Actions action = new Actions(driver);
action.SendKeys(pObjElement, Keys.Space).Build().Perform();
Thread.Sleep(TimeSpan.FromSeconds(2));
var dialogHWnd = FindWindow(null, "Elegir archivos para cargar"); // Here goes the title of the dialog window
var setFocus = SetForegroundWindow(dialogHWnd);
if (setFocus)
{
Thread.Sleep(TimeSpan.FromSeconds(2));
System.Windows.Forms.SendKeys.SendWait(pFile);
System.Windows.Forms.SendKeys.SendWait("{DOWN}");
System.Windows.Forms.SendKeys.SendWait("{TAB}");
System.Windows.Forms.SendKeys.SendWait("{TAB}");
System.Windows.Forms.SendKeys.SendWait("{ENTER}");
}
Thread.Sleep(TimeSpan.FromSeconds(2));
}

Categories

Resources