Reputation: 1320
I'm newbie to C# Selenium. I tried to automate an "sign in" and "sign out" of an LinkedIn Application. For that I have written the below code,
Here the "Sign out" is an hidden element.
My code
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.linkedin.com/");
driver.Manage().Window.Maximize();
driver.FindElement(By.Id("login-email")).SendKeys("valid email ID");
driver.FindElement(By.Id("login-password")).SendKeys("valid password");
driver.FindElement(By.Name("submit")).Click();
Actions action = new Actions(driver);
action.MoveToElement(driver.FindElement(By.XPath("//*[@id='img-defer-id-1-6775']"))).Build().Perform(); // Getting an exception here
Thread.Sleep(3000);
driver.FindElement(By.XPath("//*[@id='account-sub-nav']/div/div[2]/ul/li[1]/div/span/span[3]/a")).Click();
But I'm getting an "No Such Element Exception". Even I tried to find the element by ID but getting the same exception. Not sure what I did wrong.
Can anyone help me.
Upvotes: 1
Views: 11363
Reputation: 955
Hoverable elements I find it's best to use JavaScript. Action Builder tends to have a high rate of failure, and will cause other hoverable elements to become visible as it scrolls through the page, causing the element that you want to become obscured. I've found this method somewhere online (can't remember where) and it works significantly better than any other method I've tried.
String javaScript = "var evObj = document.createEvent('MouseEvents');" +
"evObj.initMouseEvent(\"mouseover\",true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);" +
"arguments[0].dispatchEvent(evObj);";
IJavaScriptExecutor executor = driver as IJavaScriptExecutor;
executor.ExecuteScript(javaScript, webElement);
Upvotes: 5
Reputation: 101
Try adding a Thread.sleep after clicking submit button. Here issue is selenium driver is searching for element even before page is loaded.Hence element not found exception is thrown.
Instead of Thread.sleep in your code ,you could use explicit waits.
Try this
IWebDriver driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
driver.Navigate().GoToUrl("https://www.linkedin.com/");
driver.Manage().Window.Maximize();
driver.FindElement(By.Id("login-email")).SendKeys("valid email ID");
driver.FindElement(By.Id("login-password")).SendKeys("valid password");
driver.FindElement(By.Name("submit")).Click();
Actions action = new Actions(driver);
wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//*[@id='img-defer-id-1-6775']")));
action.MoveToElement(driver.FindElement(By.XPath("//*[@id='img-defer-id-1-6775']"))).Build().Perform();
wait.Until(ExpectedConditions.ElementIsClickable(By.XPath("//*[@id='account-sub-nav']/div/div[2]/ul/li[1]/div/span/span[3]/a"))).Click();
Upvotes: 4
Reputation: 2444
If the element you are trying to click is invisible, you can make it visible trough JavaScript and then click on it:
((IJavaScriptExecutor)Driver).ExecuteScript("THE ELEMENT YOU WANT TO CLICK.hidden = false;", element);
element.click
Upvotes: 0