I'm new to Selenium and try to automatically open a website in Full Screen mode.
The website has a Login which is already working with Selenium.
After the login only one button has to be pressed.
Hereby an WebdriverTimoutException is thrown in the second last line.
The InnerException says NoSuchElementException.
But when I open the web console, I can see the button.
IWebDriver driver = new EdgeDriver(System.IO.Directory.GetParent(System.IO.Directory.GetParent(Environment.CurrentDirectory).ToString()).ToString() + "\\webdriver");
driver.Navigate().GoToUrl(#"http://examplehomepage.com");
driver.FindElement(By.Id("username")).SendKeys("abc");
driver.FindElement(By.Id("password")).SendKeys("password123");
driver.FindElement(By.TagName("button")).Click();
driver.Manage().Window.FullScreen();
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
IWebElement element = wait.Until(ExpectedConditions.ElementToBeClickable(By.TagName("button")));
element.Click();
I tried it with:
Edge (85.0.564.44)
Chrome (85.0.4183.83)
Firefox(80.0.1).
You can use sleep to delay the interaction
You are using ElementToBeClickable. You should also use ElementToBeVisible and also check whether element is enabled as well from isEnabled function.
Related
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.
When I try to use the selenium click in firefox, it doesn't wait till the element load completes.
Has anybody faced a similar problem?
First you should define default timeout of Webdriver after initialization as following :
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
After that you can wait that web element before clicking it like following :
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromMilliseconds(10));
wait.Until(ExpectedConditions.ElementIsVisible(By locator));
I'm trying to automate Paypal withdrawals by using C# and Selenium. The application logs into Paypal using provided credentials and clicks on the 'transfer money' link, which then shows a pop-up (which looks to be an iframe). My problem is that I can't click on any of the elements in the pop-up, and I've tried every suggestion I could find.
Here is a screenshot of the form and the underlying html:
paypal form
I'm trying to click on the 'From' dropdown and among other things I've tried:
driver.FindElement(By.XPath("//*[#id=\"selection-container\"]/form/section/table/tbody/tr[2]/td/div[1]/div[1]")).Click();
and
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].hidden = false;", driver.FindElement(By.XPath("//*[#id=\"selection-container\"]/form/section/table/tbody/tr[2]/td/div[1]/div[1]")));
but either get and 'Unable to locate element' or 'Element not visible' errors. How do I get to the 'From' input element on the pop-up? (If you're using paypal, you could also log in and take a peek at the pop-up if needed).
You need to switch to the iframe first
IWebElement frame = driver.FindElement(By.TagName("iframe")); // locate the iframe element
driver.SwitchTo().Frame(frame);
driver.FindElement(By.XPath("//*[#id=\"selection-container\"]/form/section/table/tbody/tr[2]/td/div[1]/div[1]")).Click();
And to switch back
driver.SwitchTo().DefaultContent();
Try
[FindsBy(How = How.CssSelector, Using = "div[class$='source-dropdown']")]
public IWebElement _ddSource;
The '$' specifies the end of the attribute, in the case the end of the class is source-dropdown
at first you need to switch to that iframe. use the below code:
IWebElement frame = driver.FindElement(By.CssSelector("iframe[src ='/moneytransfer']");
driver.SwitchTo().Frame(frame);
now you can click on that pop up by using this cssSelector:
div[class$='source-dropdown']
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().
I try to click on the login button(Đăng nhập) to show up the login box, but fail to achieve it.
The loginbox just doesn't show up.
Selenium, webdriver are all latest version
using (IWebDriver driver = new ChromeDriver())
{
driver.Navigate().GoToUrl("http://sinhvienit.net/forum/");
// driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));
// driver.FindElement(By.XPath("//a[#href='#loginform']//span")).Click();
// driver.FindElement(By.XPath("//a[#href='#loginform']")).Click();
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.Until(ExpectedConditions.ElementExists(By.XPath("//a[#href='#loginform']"))).Click();
wait.Until(ExpectedConditions.ElementExists(By.XPath("//a[#href='#loginform']//span"))).Click();
wait.Until(ExpectedConditions.ElementExists(By.Id("navbar_username")));
wait.Until(ExpectedConditions.ElementExists(By.Id("navbar_password")));
// var loginBox= wait.Until(ElementIsClickable(By.Id("loginform"))); >> fail
driver.Scripts().ExecuteScript("document.getElementById('navbar_username').style.display='inline';");
driver.Scripts().ExecuteScript("document.getElementById('navbar_password').style.display='inline';");
Console.ReadKey();
}
C# extension:
public static IJavaScriptExecutor Scripts(this IWebDriver driver)
{
return (IJavaScriptExecutor)driver;
}
There are 2 problems.
1- There's a webpage coming up prior to the actual forum page, when you are navigating to the site. Below is the image for that:
So you have to click on the button, that is highlighted above first. And, then after you will be able to navigate to the forum's page.
2- Your button is certainly getting clicked, but since the webpage has not properly loaded, the click action is not proceeding.
Hence, you need to wait for certain element that gets loaded when the page is loaded properly.
Below code will help you out:-
using (IWebDriver driver = new ChromeDriver())
{
driver.Navigate().GoToUrl("http://sinhvienit.net/forum/");
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30)); //Give the implicit wait time
driver.FindElement(By.XPath("//button[#id='btnSubmit1']")).Click();// Clicking on the button present in prior page of forum
//Waiting till the element that marks the page is loaded properly, is visible
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.Until(ExpectedConditions.ExpectedConditions.ElementIsVisible(By.XPath("//*[#id='vtlai_topx']/a")));
driver.FindElement(By.XPath("//a[#href='#loginform']")).Click();
...
You can proceed with rest then.