Selenium C# dropdown jumps back to original state - c#

Currently I am working on a little project to verify some login accounts.
I run into this problem that when i select the provider Canal Digitaal and then click 'Login', the dropdown box jumps back to it's initial state. When doing manually its working fine but when automating with a UnitTest it doesn't. I simplified the code (without POM) and its still not working. Hopefully someone knows what my problem is. For a similar site its working fine but for this it aint.. This is my code now:
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://video.sport1.nl");
driver.FindElement(By.Id("login")).Click();
new SelectElement(driver.FindElement(By.Id("provider"))).SelectByValue("canaldigitaal");
driver.FindElement(By.Id("login")).Click();

The problem is that there are two elements on the page with ID = login. You can see that by using driver.FindElements(By.CssSelector("#login")).Count and it returns 2. One is the DIRECT INLOGGEN link (top right) and the other is Inloggen (red button). You are mistakenly clicking the DIRECT INLOGGEN link which resets everything. Try this
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://video.sport1.nl");
driver.FindElement(By.Id("login")).Click();
new SelectElement(driver.FindElement(By.Id("provider"))).SelectByValue("canaldigitaal");
driver.FindElement(By.CssSelector("div.login > #login")).Click();
I grabbed a parent DIV first div.login (DIV with class login) then found the A child (>) with ID login.

Related

C# Selenium WebDriver - Is it possible to check if a certain webpage is already opened in the browser?

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.

Google Custom Search Results - Link target blank does not go to new browser tab/window

I display the Google custom search results for my site onto a new page. I would like the each search result, when clicked, to go to a new browser tab/window.
From my understanding of Google GCSE, clicking on a result should automatically go to a new browser window. The Search Results linkTarget defaults to _blank. I added the explicit code anyways.
<div class="searchresults-only" data-linkTarget="_blank"></div>
<gcse:searchresults-only linkTarget="_blank"></gcse:searchresults-only>
What I see is that clicking on a search result stays in the same browser window and does not go to a new one.
What am I doing wrong?
<div class="searchresults-only" data-linkTarget="_blank"></div>
Div code is not neccessary for this task
try this code
<gcse:searchresults-only linkTarget="_parent"></gcse:searchresults-only>
it will help you.
just replace linkTarget="_blank" to linkTarget="_parent" and run it..

Selenium WebDriver SubMenu click not working

I am not able to Click on SubMenu item using selenium webdriver using c#.
I am using IE9 and FireFox 13.
I have tried Action Builder but it does not work.
It gives an error saying element cannot be clicked.
WebDriverWait Wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(5));
IWebElement menu = Wait.Until((d) => webDriver.FindElement(By.Id("id1")));
IWebElement menuOption = Wait.Until((d)=>webDriver.FindElement(By.Id("ID2")));
Actions builder = new Actions(webDriver);
builder.MoveToElement(menu).Build().Perform();
Thread.Sleep(5);
//then click when menu option is visible
menuOption.Click();
I have used even javascript :
js.ExecuteScript("return $(\"a:contains('ID1')\").mouseover();"); // Mouse hove to main menu
webDriver.FindElement(By.Id("ID2")).Click();
Please give some solution to click on hidden elements
You could use Expected Conditions to wait for element being clickable after hovering above it (Thread.sleep() is almost always the bad choice. And 5 ms won't be enough.).
The docs for this class (ExpectedConditions in OpenQA.Selenium.Support.UI namespace) are broken as I can see them now, but if you can follow the Java code in the link above, here are the Expected conditions for Java - it's really almost the same in C#, too.
Instead of using the statement Thread.sleep(). You can try to click on the element after making sure that it is displayed.
After you get the WebElement you want to click on , check if it is displayed by using the isDisplayed() method within the ExpectedContition statement about which #Slanec is talking about in the above post.
By this you can make sure you will click on the element only after Wait.Until() returns true. i.e the menuOption is displayed.
I'm writing the code in java as I do not know C#. But I guess you can figure out what I'm trying to say -
new WebDriverWait(driver, 60).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver ) {
return driver.findElement(By.Id("ID2")).isDisplayed();
}
});
I hope that this helps you.

IE cannot work with C# Selenium script, while Firefox works well

Scripts:
IWebDriver ie = new InternetExplorerDriver();
IWebDriver ff = new FirefoxDriver();
string baseURL = "http://xxxxxxxxxxxx";
ISelenium iesele = new WebDriverBackedSelenium(ie, baseURL);
ISelenium ffsele = new WebDriverBackedSelenium(ff, baseURL);
The page of baseURL has 2 frames and the upper frame is a warning page, and I want to select "Agree" then Click on "OK" to close it. Scripts of IE doesn't work, can discover the object of the checkbox and button, but "Select" and "Click" doesn't work. But under Firefox, it works, the upper frame was closed successfully.
Script:
IE
iesele.Start();
iesele.Open(baseURL);
iesele.SelectFrame("UpperFrame");
iesele.FindElement(By.Name("agree")).Click();
iesele.FindElement(By.CssSelector("ok")).Click();
Firefox
ffsele.Start();
ffsele.Open(baseURL);
ffsele.SelectFrame("UpperFrame");
ffsele.FindElement(By.Name("agree")).Click();
ffsele.FindElement(By.CssSelector("ok")).Click();
Does anyone know why IE cannot execute this script correctly?
Should I set something of IE8 ?
thanks
Hmm i have searched around for your issue; maybe you also have the same issue as in this stackoverflow question:
Selenium 2.0b3 IE WebDriver, Click not firing
Here it seems the given frame needs to have focus (via a click()) before the actual click() is registered on your element.
There are differences in the way each browser renders a page and also differences in the Selenium drivers (so it may not be possible to use exactly the same script for different browsers).
You might find this answer to a similar question useful. In particular, try selecting and clicking on a parent of the target element (such as a <div>) instead of the element itself. And also, try using MouseDown() followed by MouseUp() instead of Click().
I have found a few times that Firefox will work on finding and interacting with an element but IE will fail at various spots. Most of the time I throw in a 'wait for element' and the IE issue is resolved. I think IE is sometimes a bit slower creating elements (or does things in a different order?) so at times the element doesn't exist when you aim to click it.
This might not be your issue, but it seems to happen to me rather frequently!
edit: I also use Chrome, and often FF and Chrome work when IE fails.

Context click in Selenium 2.2

Got problem with opening custom context menu using advanced user interactions API in Selenium 2 (http://code.google.com/p/selenium/wiki/AdvancedUserInteractions).
Here is C# code:
var driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://www.flickr.com/photos/davidcampbellphotography/4581594452/");
IWebElement photoDiv = driver.FindElement(By.Id("photo"));
Actions actions = new Actions(driver);
var context = actions.ContextClick(photoDiv).Build();
context.Perform();
Still this code doesn't open context menu but simply clicks on the image.
Still many other interactions work fine, like those at this blogpost.
I'm using Firefox 5 on Windows XP, Selenium version 2.2.
Thanks in advance for any suggestions how to make it work.
UPDATE:
However, the code from here does the same (left click instead of context click).
ILocatable loc = (ILocatable)photoDiv;
IMouse mouse = ((IHasInputDevices)driver).Mouse;
mouse.ContextClick(loc.Coordinates);
mouse.MouseMove(loc.Coordinates, 15, 15);
Looks like a bug.
Have you tried using a robot click, Although they're not the most efficient way of doing so, but as long as the browser window is the top window it'll click the co-ordinates you set
Robot robot = new Robot();
robot.mouseMove(650, 590);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
Maybe this will help you :-)
This seems to be that bug.
It is stated in the user group here, though the bug is about double click.

Categories