Failed to locate element bind to an iframe - c#

I am trying to automate login of following site:
https://www.sbs.com.au/ondemand/
after pressing Signin/Signup its opening login modal which is an iframe.
I am trying to locate email,password, login button with following code but not working. Its opening modal but after that doing nothing just test failing but no error message either.
Code for clicking Signin/signup link and switch to iframe:
var link = Driver.Instance.FindElement(By.Id("capture_signin_link"));
link.Click();
Driver.Instance.Manage().Timeouts().ImplicitWait.Seconds.Equals(10);
Driver.Instance.SwitchTo().Frame(1);
Code for passing value in email,password,and clicking login button:
var inputus = Driver.Instance.FindElement(By.Name("email"));
inputus.SendKeys(username);
Driver.Instance.Manage().Timeouts().ImplicitWait.Seconds.Equals(5);
var inputpass = Driver.Instance.FindElement(By.Name("password"));
inputpass.SendKeys(password);
Driver.Instance.Manage().Timeouts().ImplicitWait.Seconds.Equals(5);
var loginbutton = Driver.Instance.FindElement(By.ClassName("traditional-login"));
loginbutton.Click();
I am not sure if I am passing correct value to find locators and ifame. Please visit above site information for DOM info.
Looking forward a good solution. Please help me

My Latest Code for clicking signup to open login modal:
var link = Driver.Instance.FindElement(By.Id("capture_signin_link"));
link.Click();
Driver.Instance.Manage().Timeouts().ImplicitWait=TimeSpan.FromSeconds(10.00);
Driver.Instance.SwitchTo().Frame(Driver.Instance.FindElement(By.Id("login-iframe")));
Driver.Instance.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10.00);
Code for login:
IWebElement inputus = Driver.Instance.FindElement(By.XPath(".//*[#id='onboarding']/div[2]/form/section[2]/div/div[1]/input"));
inputus.SendKeys(username);
Driver.Instance.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10.00);
var inputpass = Driver.Instance.FindElement(By.Name("password"));
inputpass.SendKeys(password);
Driver.Instance.Manage().Timeouts().ImplicitWait=TimeSpan.FromSeconds(10.00);
var loginbutton = Driver.Instance.FindElement(By.ClassName("traditional-login"));
loginbutton.Click();
Above code worked for Chrome. On firefox its showing "Array out of index error" on Sendkeys() method. I found on some blog a lot of people are facing same problem and suggesting to downgrade firefox browser version to get proper support from getco driver as getco driver failing to handle sendkeys().
I haven't try above suggestion. Will try and update here. Till then if you have any other suggestion on that plz let me know.

Related

Unable to click Sign in button of Microsoft Login in C# Selenium

Trying to click Sign In button but getting "Stale element Exception error".
IWebElement email = driver.FindElement(By.XPath("//*[contains(#name,'loginfmt')]"));
IWebElement password = driver.FindElement(By.XPath("//*[contains(#name,'passwd')]"));
IWebElement signIn = driver.FindElement(By.XPath("//*[contains(#id,'idSIButton9')]"));
IWebElement signInbtn = driver.FindElement(By.XPath("//*[#id='idSIButton9']"));
IWebElement signInbtn1 = driver.FindElement(By.XPath("//input[#type='submit']"));
email.SendKeys("automate#outlook.com");
email.SendKeys(Keys.Enter);
Thread.Sleep(1000);
password.SendKeys("Abc*123$");
password.SendKeys(Keys.Enter);
signInbtn1.Click();
Error:
OpenQA.Selenium.StaleElementReferenceException : stale element
reference: element is not attached to the page document
The reason of getting the StaleElementReferenceException is that the driver have found the element, but the page refreshed by the moment, you try to interract it, so element state is stale.
Try to initialize the element signInbtn1 after you fill the password field:
password.SendKeys("Abc*123$");
password.SendKeys(Keys.Enter);
IWebElement signInbtn1 = driver.FindElement(By.XPath("//input[#type='submit']"));
signInbtn1.Click();
I wasn't using C# for the test script, but I'm calling the selenium driver via javascript (within a jest test) and was getting the same problem for that specific Login Modal you mentioned.
Looking at the HTML in dev tools, I found that there is a subtle issue when using the ID ("idSIButton9") as the button element will change once the email has been entered in.
ie. The button element's value will change from 'Next' to "Sign in"
I found that using the id twice to identify the same button element results in the stale element issue.
So, for the second time round, I found the button element using the following xpath
"//input[#value='Sign in']"
This is much more specific than the id in this case.
Hope that helps.

Getting the first 5 links of a Google search

I'd like to make a simple console application, similar to a search engine.
It asks the user what to search, finds results on google, and then the user can type "Click on the first/second/third link(etc)" and the application automatically clicks on that link.
I've tried using Selenium and I got to the point where I could initiate the search and find the results, but I didn't know how to actually click on a link. I've also tried using mouse coordinates, but the code is too complicated for me.
I've also tried to find a way to extract the links of the first 5 google results and THEN by some way click them.
Anyway, I've been searching for hours, but I haven't found anything yet. It would be amazing if you guys could help me out!
Here's the code:
static void Main(string[] args)
{
Console.WriteLine("Search for:");
string command = Console.ReadLine();
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://www.google.com");
driver.Manage().Window.Maximize();
IWebElement searchInput = driver.FindElement(By.Id("lst-ib"));
searchInput.SendKeys(command);
searchInput.SendKeys(Keys.Enter);
}
If it's a console app, you won't be able to click a link because the page won't be displayed. You could grab the links off the search results page but then how would you display the page after you navigate??? I don't guess I understand what the overall goal of your project is.
If all you want is to grab the links off the google search results page you can use the code below. It will grab the URLs of all of the search results on the page. You can then display how ever many of the links on the screen and then after the user chooses a link, navigate (something?) to the desired URL.
IWebElement searchInput = Driver.FindElement(By.Id("lst-ib"));
searchInput.SendKeys(command);
searchInput.SendKeys(Keys.Enter);
WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(5));
By linkLocator = By.CssSelector("cite._Rm");
wait.Until(ExpectedConditions.ElementToBeClickable(linkLocator));
IReadOnlyCollection<IWebElement> links = Driver.FindElements(linkLocator);
foreach (IWebElement link in links)
{
Console.WriteLine(link.Text);
}

Navigate driver to new opened window in selenium with C#

I wrote a simple code to submit a sign up form with Selenium. Before submit, driver should come from home page to sign up page.
var firefox = new FirefoxDriver();
firefox.Navigate().GoToUrl("http://mywebsite/home");
If I print firefox.Title, it shows me title of home page currectly
And in home page, there is a sign-up button. Sign up button link is bellow.
<a target="_blank" href="SignUp.jsp">Register Here</a>
To navigate to sign up page, I wrote a line:
firefox.FindElement(By.CssSelector("a[href='SignUp.jsp']")).Click();
After this, driver shows me the sign up page in new window of firefox browser. To navigate driver to the sign up I wrote firefox.Navigate();
Now If I print firefox.Title, it shows me title of home page again.
Please help me to find out problem. Thanks in advance.
You pretty much grabbing the same title since the you never switched to newly opened window
// Get the current window handle so you can switch back later.
string currentHandle = driver.CurrentWindowHandle;
// Find the element that triggers the popup when clicked on.
IWebElement element = driver.FindElement(By.XPath("//*[#id='webtraffic_popup_start_button']"));
// The Click method of the PopupWindowFinder class will click
// the desired element, wait for the popup to appear, and return
// the window handle to the popped-up browser window. Note that
// you still need to switch to the window to manipulate the page
// displayed by the popup window.
PopupWindowFinder finder = new PopupWindowFinder(driver);
string popupWindowHandle = finder.Click(element);
driver.SwitchTo().Window(popupWindowHandle);
// Do whatever you need to on the popup browser, then...
driver.Close();
driver.SwitchToWindow(currentHandle);
And, after switching to new window you should get new title.
However, this window handles process is utterly confusing to me. Selenium .Net bindings provide PopupWindowFinder class to handle windows.
Gratitude to JimEvans for his nice works and this
Use
firefox.SwitchTo().Window(handle);
where handle is one of instances found in firefox.WindowHandles. This will switch between the different window instances. You can find more information in the docs for IWebDriver.SwitchTo().

How to open a link in new tab using javascript

I am working on a website, in which I have to open a url from backend. I am using c# now. My problem is that I want to open link in new tab instead of new window.
My code is here:-
string url = ppHref.ToString();
string newScript = "<script language='javascript'>window.open('" + ppHref.ToString() + "', '_blank');</script>";
ClientScript.RegisterStartupScript(this.GetType(),"OpenUrl", newScript);
Can Anybody tell me how to open this url in new tab. I don't like pop ups so I don't wanna use window.open(). Please help me.
Thanks in Advance
Sample Code
You can use:
window.open(url,'_target')
_target opens the page in next tab.
Html Code :
<button onclick="OpenInNewTab();" type="submit">
Javascript Code :
function OpenInNewTab(url) {
var win = window.open(url, '_blank');
win.focus();
}
Change Your Browser Setting.
In FireFox ,
Go To Options -> Tab setting and Check "Open New Windows in a New Tab instead" setting.
This Solution Worked For me .
I think answer to this question will help , Open a URL in a new tab using JavaScript https://stackoverflow.com/a/30512485/2293686
try like this,
$('#myButton').click(function () {
var redirectWindow = window.open('url', '_blank');
redirectWindow.location;
});

WatiN: MsHtmlBrowser will not TypeText

From the WatiN website:
// Open a new Internet Explorer window and
// goto the google website.
IE ie = new IE("http://www.google.com");
// Find the search text field and type Watin in it.
ie.TextField(Find.ByName("q")).TypeText("WatiN");
// Click the Google search button.
ie.Button(Find.ByValue("Google Search")).Click();
// Uncomment the following line if you want to close
// Internet Explorer and the console window immediately.
//ie.Close();
The above sample works just fine. However, since I do not want to open up a browser window, I modified the above code to use MsHtmlBrowser:
// goto the google website.
var ie = new MsHtmlBrowser();
ie.GoTo("http://www.google.com");
// Find the search text field and type Watin in it.
ie.TextField(Find.ByName("q")).TypeText("WatiN");
// Click the Google search button.
ie.Button(Find.ByValue("Google Search")).Click();
The TypeText line is throwing an exception. Any idea what's wrong?
The MsHtmlBrowser is only there to find elements and read their attribute values. There is no support for clicking a link, typing text, firing events, no session state or any other way of interact like with a normal browser. So us it for scrapping only.
HTH,
Jeroen

Categories

Resources