Reputation: 3437
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;
}
Upvotes: 1
Views: 550
Reputation: 4424
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.
Upvotes: 1