I am using selenium tool in my C# windows application,
driver.Navigate().GoToUrl(baseURL);
when the the application executing this line the page loads takes 2mins.
During that time, the followin element has found within 10 sec.
driver.FindElement(By.Id("searchTerm"))
I used driver.FindElement(By.Id("searchTerm")).SendKeys(Keys.Escape);
But it wont work properly. The problem is after complete the page load then only the control execute next line . But I need to stop the page if the element has found.
Thanks
You could try issuing the Escape press at the top level, not on the element itself:
if (driver.FindElement(By.Id("searchTerm")) != null) {
Actions action = new Actions(driver);
action.SendKeys(Keys.Escape);
}
That seems to be the most common way of issuing a page load stop.
To stop page load at any moment you can use something like this:
IJavaScriptExecutor js = Brwsr.Instance as IJavaScriptExecutor;
js.ExecuteScript("window.stop();");
Related
I'm trying to select a value from the drop down 'ddl_settpymtaction' but selenium cannot locate it within the modal that resides in.
CSS:
Selenium code
driver.FindElementById("btn_SettlementNew").Click();
var Action = driver.FindElementById("ddl_SettPymtAction");
var SelectElement2 = new SelectElement(Action);
SelectElement2.SelectByValue("EFT");
In selenium you should ALWAYS add some delay when performing async operations (such as a modal which loaded with animations + ajax calls)
Why? To get your page a chance to render itself and let selenium wait for the right time to inspect the page (after being rendered with the required elements)
I'm trying to run some automated tests in Sitecore 8.1 using Chrome and Selenium and c#. My code doesn't want to find any elements within the Sitecore pages, specifically the experience editor.
I am encountering the "unable to locate element" warning.
For eg: an item I want to .Click() is the toolbar ribbon button to expose the toolbar menu.
Here's the element:
<a data-sc-id="QuickRibbon" data-sc-click="trigger:button:toggleshow" data-sc-command="" data-sc-сontrolstaterequest="" data-sc-controlstateresult="" data-sc-postponedcall="" data-sc-ispressed="false" class="sc-quickbar-item sc-quickbar-button sc_QuickbarButton_53 data-sc-registered" title="Toggle the ribbon." data-sc-pagecodescriptfilename="" data-bind="ispressed: isPressed, visible: isVisible, click: click, command: command, enabled: isEnabled" data-sc-require="/-/speak/v1/ribbon/QuickbarButton.js" href="#" style="float:right"><img src="/sitecore/shell/client/Speak/Assets/img/Speak/Common/16x16/white/navigate_down.png" alt="Toggle the ribbon."></a>
Here's its XPath:
/html/body/div/div/div[1]/nav[1]/a[3]
I have extended the wait time to allow it to become visible as it can take a few seconds to load these pages. But this didn't work.
I have tried:
driver.FindElement(By.XPath("/html/body/div/div/div[1]/nav[1]/a[3]/img")).Click();
which gave me the "unable to locate" error
driver.findElement(By.className("class="sc-quickbar-item sc-quickbar-button sc_QuickbarButton_53 data-sc-registered"")).Click();
which gave me an error about unable to use compounded classnames.
I've tried a whole host of other options/combinations trying to pick up the alt text etc but I just can't get it to pick up the element.
Any ideas? Let me know if you need any more info.
Thanks
Change this line
driver.findElement(By.className("class="sc-quickbar-item sc-quickbar-button sc_QuickbarButton_53 data-sc-registered"")).Click();
to below line :-
driver.findElement(By.className("sc-quickbar-item sc-quickbar-button sc_QuickbarButton_53 data-sc-registered")).Click();
Edited 1..
If compound class does not work here you can perform action by using xpath as below :-
var wait = new WebDriverWait(driver, TimeSpan.FromMinutes(1));
var clickableElement = wait.Until(ExpectedConditions.ElementToBeClickable(By.xpath("//a[#data-sc-id='QuickRibbon']")));
clickableElement.Click();
Edited 2..
You need to switch frame before perform action if your element is present inside a frame as below :-
driver.SwitchTo().Frame("your frame name or id");
Hope it will work...:)
following suggestions from #Software_engineer I have managed to write this which works:
Thread.Sleep(6000);
driver.SwitchTo().Frame(driver.FindElement(By.Id("scWebEditRibbon")));
var wait = new WebDriverWait(driver, TimeSpan.FromMinutes(1));
var clickableElement = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//a[#data-sc- id='QuickRibbon']")));
clickableElement.Click(); //click to drop down the toolbar
driver.SwitchTo().DefaultContent();
I needed to switch to the iframe!
I was wondering if it is possible to check with C# and selenium web driver if a certain webpage is opened in the default browser?
My idea is to link certain ticketing system's time tracker with toggl.
For instance - on click of the "Time Track" button in the ticketing system, the program to click the toggl start button programmatically, at the same time.
Yes, it's possible.
You can devise a solution that checks the default window's URL or title.
if (driver.Url == 'http://some_url') { /* you are there */ }
or
if (driver.Title == 'Some Title') { /* the window is open and currently there */ }
Now, if you are running a browser manually using your own browser, then expect Selenium to detect that, then i'm sorry, but that is not possible.
In addition to #sircapsalot answer:
This won't be enough since the goal is to
on click of the "Time Track" button in the ticketing system
First you should be sure that the page has been loaded and the IWebElement is clickable. Without going in some advanced usage (like JS validation of the page state), this should do the work just fine:
var wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(15));
var myElement = wait.Until(x => x.FindElement(By.Id("timeTrackBtnId")));
if(myElement.Displayed)
myElement.Click();
Then go for the
program to click the toggl start button programmatically, at the same time
I'm not sure how you'll sync and how page's JS events will handle this simultaneous actions, but you can try with System.Threading. If the page is created by you maybe this second part (click button, click togl) is better to be handled in the JS code.
I'm automating tests for our webapp in Selenium WebDriver for C#. One of our test scenarios identified an issue with clicking the save button multiple times resulting in multiple identical records.
The standard IWebElement.Click() causes Selenium to block until the page is fully loaded. That means by the time our second click comes around to executing, the postback has been performed and we're not on the form page anymore.
Does anyone know a means of 'manually' clicking an element that won't cause Selenium to block?
You could either wait for a predetermined amount of time for the page to load:
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
...or to be more dynamic and wait for your button to appear:
var driver = new WebDriver();
var wait = new WebDriverWait(driver, TimeSpan(0, 1, 0));
wait.Until(d => d.FindElement(By.Id("button"));
Source: https://stackoverflow.com/a/7811812/2006048
Selenium also has source code that is similar to the second method: http://selenium.googlecode.com/svn/trunk/dotnet/src/WebDriver.Support/UI/ExpectedConditions.cs
Let me know if it works out for you. I personally use these options with WatiN:
browser.WaitForComplete();
...or:
browser.WaitUntilContainsText("Text");
It's a shame Selenium does not have the first one.
If we use JavaScript to send our click events, Selenium will not be blocked and we can click multiple times. However, because our click triggers a page load, we cannot reference the element directly. Instead, we need to specify the location to click and then fire our click events.
Because our WebApp uses JQuery, I was able to use the code specified here:
How to simulate a click by using x,y coordinates in JavaScript?
So in the end our C# logic looks something like this:
IWebElement element = driver.findElement(By.id("foobar"));
Point point = element.Location;
IJavascriptExecutor jscript = (IJavascriptExecutor)driver;
jscript.executeScript("$(document.elementFromPoint(arguments[0], arguments[0])).click();", point.X, point.Y);
Although this sends the click event I'm not 100% sure that the element receives it; I'll run some experiments and see.
What you need to do is click via javascript. In java this is done like this:
IJavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", driver.findElement(By.id("gbqfd")));
executor.executeScript("arguments[0].click();", driver.findElement(By.id("gbqfd")));
I actually imagine it is very similar, this will not block selenium and you should be able to chain a few on before the page comes back.
if this newer approach is too slow you might be quicker doing it all in js, e.g.
IJavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("document.getElementById(id).click();");
executor.executeScript("document.getElementById(id).click();");
I have a WebBrowser element in my form.
It loads a page.
This is followed by code to wait until page is loaded:
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
Then I make a sequence of GetElementByID.Focus statements.
Each of them is followed by command "SendKeys.Send("MyKeys")"
But when I run the program, web browser downloads needed a page, and all the "SendKeys" values appear to be typed in the last field.
Tried to pause it in many ways before filling in the fields, doesn't work.
Please try this
webBrowser1.Document.GetElementById("FirstName").SetAttribute("value", "John");
let me know how it goes.