C# Selenium Can't Locate to any Element - c#

Why C# + Selenium can't locate to any element from this URL only
I want to try to filling data to textbox programmatically using c# + selenium.
I had tried for some sites and it worked, but only that site it doesn't work. This is my code.
class Program
{
static void Main(string[] args)
{
Test test = new Test();
test.testDriver();
Console.WriteLine(Environment.NewLine + "Done!");
Console.ReadLine();
}
}
public class Test
{
public void testDriver()
{
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl(#"http://atrium.xsis.co.id/#!/");
IWebElement login = driver.FindElement(By.Name("email")); //Unable to locate element
//IWebElement login = driver.FindElement(By.Id("email")); //Unable to locate element
//IWebElement login = driver.FindElement(By.Name("password")); //Unable to locate element
//IWebElement login = driver.FindElement(By.Id("password")); //Unable to locate element
//IWebElement login = driver.FindElement(By.XPath("//input[#id='password']")); //Unable to locate element
//IWebElement login = driver.FindElement(By.XPath("//input[#id='email']")); //Unable to locate element
//IWebElement login = driver.FindElement(By.XPath("//input[#name='password']")); //Unable to locate element
//IWebElement login = driver.FindElement(By.XPath("//input[#name='email']")); //Unable to locate element
login.SendKeys("MyName");
login.Submit();
//------------------------------Some sites, i have tried
/* --It Worked
driver.Navigate().GoToUrl(#"https://accounts.google.com/");
IWebElement login = driver.FindElement(By.Name("identifier"));
login.SendKeys("MyName");
login.Submit();
*/
/* --It Worked
driver.Navigate().GoToUrl(#"https://web.facebook.com/");
IWebElement login = driver.FindElement(By.Name("email"));
login.SendKeys("MyName");
login.Submit();
*/
/* --It Worked
driver.Navigate().GoToUrl(#"https://stackoverflow.com/");
IWebElement login = driver.FindElement(By.Name("display-name"));
login.SendKeys("MyName");
login.Submit();
*/
/* --It Worked
driver.Navigate().GoToUrl(#"https://github.com/login?");
IWebElement login = driver.FindElement(By.Name("login"));
login.SendKeys("MyName");
login.Submit();
*/
}
}
I think that is simple code, just locate specific element. Or use other method if the web using IFrame, but there is no frame.
I am using vs 2013 community edition, WebDriver.dll version 3.4.0.0, RunTime version v4.0.30319., and Firefox 54.0.1 (32-bit).

There's something which is kind of cool and annoying at sometime in Selenium, it literally try to work exactly as if the user where using the browser. Hence you gotta be sure that the element exposed on your screen and the user can click on it, otherwise you must scrool until it.
Another important thing is the bootstrap, which sometimes change the visibility of its elements and replace to others.
Scrool to elements on page's bottom.
Guarantee the element is literally visible.
Check whether there's something over the element.

I can't comment yet and I'd prefer that this be a comment, but here goes anyway.
I tried browsing to the site you reference and can't get there. Not sure if it's live or not or just blocked by my company's firewall. I really wanted to look at the code for the site because I've seen issues like this before. Typically when I've seen this it's either a locater issue or an iframe issue. If you could post the html from the site that would help diagnose your issue. Oh, and the code you have written looks correct, but that doesn't help if Selenium is looking in the wrong frame.
So, here's what I would try.
First, I'd use FireFox's developer version and inspect the element. Make sure it's not inside of a frame that you haven't noticed.
Second, I'd try to locate by CSSSelector. I know that it should work by Id or XPath, but sometimes it doesn't and I don't know why. However, I have had luck with locating by CSSSelector when the others fail. You can copy the CSS Selector directly out of the inspection window in FireFox's developer version.
Third, if none of that works, I'd pass a SwitchTo the Default Content Frame, just in case the entire page is in a frame that is buried inside a div that you're missing. The code for that is:
driver.SwitchTo().DefaultContent();
Finally, the last thing I would try is adding some explicit waits just in case there is an element loading after you are trying to access it. Unfortunately this happens more than I'd like.
This would be my final, kitchen sink, approach.
Thread.Sleep(2000);
driver.SwitchTo().DefaultContent();
Thread.Sleep(2000);
By emailLocator = By.CSSSelector("insert CSS Selector Here");
IWebElement email = driver.FindElement(emailLocator);
email.SendKeys("email address");
If that still doesn't work, please post the html for the page you're trying to access and we'll see if we can figure out why it's failing to locate the element.

Your code looks good... something simple like your first attempt, By.Id("email"), should have worked. When it doesn't you want to look first to see if the desired element is in an IFRAME. In this case, it isn't.
The next thing you should try is an explicit wait, WebDriverWait.
IWebElement email = new WebDriverWait(Driver, TimeSpan.FromSeconds(3)).Until(ExpectedConditions.ElementIsVisible(By.Id("email")));
email.SendKeys("MyName");
email.Submit();
The wait.Until() returns the element waited for so you can capture that by assigning the return to a variable and then using it, as I did above.

Have you tried using Xpath?
driver.findElement(By.xpath("//input[#name='email']"))
I find that sometimes Selenium is finicky and you need to try other approaches to find elements.

Related

C# Selenium fails to correctly assert

Test below fails, saying that xpath expression is not correct. I did it according to online tutorials.
// open browser
IWebDriver webDriver = new FirefoxDriver();
// navigate to site
webDriver.Navigate().GoToUrl("https://localhost:44311/");
// identify details button
IWebElement detailsLink = webDriver.FindElement(By.XPath("//*[contains(., 'Details')]"));
// operation
detailsLink.Click();
var detailsLabel = webDriver.FindElement(By.XPath("//div[text() = 'Change in $'"));
// assertion
Assert.That(detailsLabel.Displayed);
I want to create simple Selenium test.
I'm pressing specific button to open new page
Checking if specific object is loaded correctly on that page
Button is <a></a> with Details inside. There are actually duplicates of that, the only unique thing is href. This might be the first issue, but I don't know how to "catch" it.
detailsLabel is actually a <div>Change in $</div> with class that I think is irrelevant to the tests.
So, my question is, how to write this test correctly? Test itself should be more or less fine, but detailsLink and detailsLabel has certain issues.
Edit
IWebElement detailsLink = webDriver.FindElement(By.CssSelector("[href*='Details?cryptocurrency=BTC']"));
//operation
detailsLink.Click();
var detailsLabel = webDriver.FindElement(By.XPath("//div[contains(text(),'Change in $')]"));
Return exception Selenium.StaleElementReferenceException for detailsLink
Edit
IWebElement detailsLink = webDriver.FindElement(By.CssSelector("[href*='Details?cryptocurrency=BTC']"));
detailsLink.Click();
var detailsLabel = webDriver.FindElement(By.XPath("//*[text()='Change in $```
Test fails at `detailsLabel`, fails to find our target `div`.
To validate if //*[contains(., 'Details')] xpath is unique you can do something like
List<WebElement>list = webDriver.FindElements(By.XPath("//*[contains(., 'Details')]"));
now, if list is empty - this means your locator is wrong and if it contains more than 1 element your locator is not unique.
But the better way is to validate this manually and directly on the webpage with F12 Inspector tool on your browser.
To locate the element containing some href without the specific href text use //href in case href is an element tag. Otherwise use //*[#href] if href is an attribute.
For the detailsLabel try using //div[contains(text(),'Change in')] xpath
I used Java syntax, but it's similar enough to C#

Unable to get elements from HTML web page

I'm trying to populate an HTML web page (http://wasitviewed.com) in C# using Selenium but no matter what i try, it always crashes when I try and find the website elements with the following error:
OpenQA.Selenium.NoSuchElementException: 'no such element: Unable to locate element: {"method":"name","selector":"href"}
I'm using the below code:
using (IWebDriver driver = new ChromeDriver())
{
driver.Navigate().GoToUrl("http://www.wasitviewed.com/");
IWebElement query = driver.FindElement(By.Name("href"));
query.SendKeys("test");
}
I've also tried using xPath but that also fails.
Is there an easier way to find the elements of the web page or is there a way i can loop through the whole web page to find the different elements and their respective id's?
In the above mentioned URL, all the elements are resides within an iframe. So, you need to move the foucs into the frame before finding the query element,otherwise NoSuchElementException will be thrown.
Whenever any element is present inside the iframe, you need to move the foucs to the frame using any one of the below startegy.
Using Index:
Index starts frame 0.So, you can switch to the frame using the index as below
driver.SwitchTo().Frame(0);//Here only one frame is available.so, Index is 0
Using Frame Name or Id:
Select a frame by its (frame) name or ID. name attributes are always given precedence over the id attributes.
driver.SwitchTo().Frame(<<Frame Name or ID>>);
Currently, frame name/id is not available in the mentioned URL.
So, you cannot use the Frame name or ID.
Using Frame WebElement:
You need to find the Frame WebElement using any one of the Locator strategies (Name,Id,ClassName,TagName,XPath,CssSelector)
and it can be used to move the foucs to the respective frame.
//Here only one frame is available.So, I have used FindElement
var frameElement= driver.FindElement(By.TagName("iframe"));
driver.SwitchTo().Frame(frameElement);
Working Code:
Option 1 :
using (IWebDriver driver = new ChromeDriver())
{
driver.Navigate().GoToUrl("http://www.wasitviewed.com/");
driver.SwitchTo().Frame(0);
IWebElement query = driver.FindElement(By.Name("href"));
query.SendKeys("test");
}
Option 2:
using (IWebDriver driver = new ChromeDriver())
{
driver.Navigate().GoToUrl("http://www.wasitviewed.com/");
var frameElement= driver.FindElement(By.TagName("iframe"));
driver.SwitchTo().Frame(frameElement);
IWebElement query = driver.FindElement(By.Name("href"));
query.SendKeys("test");
}
Your required <input> element is inside an iframe. You need to switch to that before performing actions on any elements inside the iframe.
IWebElement yourIFrame= driver.FindElement(By.XPath("//iframe"));
driver.SwitchTo().Frame(yourIFrame);
Now, this should work -
IWebElement query = driver.FindElement(By.Name("href"));
query.SendKeys("test");
Also, once you are done dealing with inside iframe, you can switch back to default content by -
driver.SwitchTo().DefaultContent();
As your element in iframe so first go to iframe then locate the element for functioning
IWebElement FirstIFrame= driver.FindElement(By.XPath("//iframe[#src='index.php']"));
driver.SwitchTo().Frame(FirstIFrame);
//Now perform operations
IWebElement query = driver.FindElement(By.Name("href"));
query.SendKeys("test");
As per your question the elements within the url http://wasitviewed.com/ are within an <iframe>. So once you access the url first you need to induce WebDriverWait for the desired <iframe> to be available and switch to it then you need to induce WebDriverWait again for the desired element to be clickable and you can use the following solution:
driver.Navigate().GoToUrl("http://www.wasitviewed.com/");
new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.XPath("//iframe[#src='index.php']")));
new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//input[#class='inputText' and #name='href']"))).SendKeys("test");
btw. when you will be confused with iframe, which iframe you should use to switch, then you can also use something like
driver.SwitchTo().DefaultContent();
or
driver.SwitchTo().Window(driver.WindowHandles.Last()); <-- this switch to the last, this is good in some case, when you are already switched in 2-3 level of iframe and you want to return. I am using with both very often.
both I am using also when I am working with pop up windows.

OpenQA.Selenium.NoSuchElementException

class Program
{
static void Main(string[] args)
{
IWebDriver driver = new ChromeDriver();
//Dev environment
driver.Url = /*URL*/;
var trialGroupName = driver.FindElement(By.Name("TrialGroupName"));
trialGroupName.SendKeys(/*trial group name */);
var userName = driver.FindElement(By.Name("UserName"));
userName.SendKeys(/*username*/);
var password = driver.FindElement(By.Name("UserPassword"));
password.SendKeys(/*password*/);
var loginButton = driver.FindElement(By.Id("Ok"));
loginButton.Click();
//Find and click on Browse tab
var browseTab = driver.FindElement(By.Id("17"));
browseTab.Click();
}
}
OpenQA.Selenium.NoSuchElementException: 'no such element: Unable to locate element: {"method":"id","selector":"17"}
(Session info: chrome=58.0.3029.110)
(Driver info: chromedriver=2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41),platform=Windows NT 6.1.7601 SP1 x86_64)'
Here's the inspect information:
<td>
<a id="17" class="defaultMenu initMenu" href="javascript:onMenuHref(17)" notran="">Browse</a>
</td>
I've also tried running it with the XPath (both the short version from Chrome and the long version from FireBug) and I receive the same error. I've tried adding in an implicit wait and a Thread.Sleep() but still received the same error. Also tried starting on a different field and tabbing to it and then it gave me the same exception but on the new field. I also tried playing around with cssSelector this morning and couldn't get that to work either. I did try to do an explicit wait but I'll be honest, I'm still trying to digest and understand it as waits are a new concept and I don't fully understand it, so I don't think I'm doing it right.
I'm a beginner, and I'm currently just trying to write the script to get it to the actual page that needs testing. I've been teaching myself some coding and selenium as I'm the only QA person at my company right now and I really think we could benefit from automation. This portion of the page isn't actually programmed by us it's done by a third party who hosts the system we use so I'm limited to what I see in the inspect.
(I commented out the user login information and URL because it is for internal use only and the page can't be reached outside of our systems anyways. Also I'm pretty sure I'm not allowed to do so by my employer).
Any help on this would be greatly appreciated, or any additional resources I can use. I've been using google mostly so there may be other pages you are aware of that could help me that I haven't found yet.
The login button takes you to a new page, which you need to wait for it to load. Try using an explicit wait to wait for the tab to be clickable. The timeout is 10 seconds in the following example, so modify that as needed.
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement browseTab = wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("17")));

How to workaround certain differences between PhantomJSDriver and ChromeDriver

I have an issue when switching from ChromeDriver to PhantomJSWebDriver, that I need a workaround for.
Using the ChromeDriver I perform a .Click on a visible element, with another visible element next to it. the .Click makes the web page place a 'popup' over these elements. I then perform a .Click on an element within this 'popup' and it then goes away, and I am able to perform a .Click on the second of the original elements and it all works fine.
However, when I switch to using the PhantomJSDriver it complains that the second element is NOT visible after the 'popup' has been removed, and so wont do the .Click.
Is this a known issue with the PhantomJS driver, or is there some way to get it to 're-evaluate' what is visible? I have tried using a DriverWait looking for the element and waiting for it to be 'Displayed' but that doesn't work
WebDriverWait waitforpopdown = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
waitforpopdown.Until(d => {
var elpd = driver.FindElement(By.ClassName(trid));
return elpd.Displayed;
});
I'm using PhantomJS 2.1.1 and Selenium 2.52.0.0 and using C#
I had the same problem with PhantomJSDriver. It hasn't the same javascript engine than Chrome, FF and IE.
I "fixed" my problem by installing a selenium server that is running chrome on a docker. If you have the ability to do that, I would recommend to work that way. It's an actual chrome browser that is running, so you don't have any difference between your different types of chromes.
You can find information about it on the following page : https://github.com/peroumal1/docker-chrome-selenium
EDIT : You have to declare your driver like this (note that it's java Syntax, but I'm sure you'll be able to find the C# syntax) :
return new RemoteWebDriver(new URL("http://myurl.url:8888"), DesiredCapabilities.chrome());

Selenium chrome driver click() method not always clicking on elements

I am writing integration tests in c# and when I use the click() method on certain elements inside a dialog box nothing happens and I get no errors. It will click some of the elements inside the dialog but not others. I thought if it wasn't selecting them properly then it would throw and exception but it runs smooth and says test passed even though it never actually clicked the button. The dialog box is an iframe.
I thought maybe it was trying to click a button that wasn't display yet or enabled so I added this before the click() call:
_driver.SwitchTo().Frame(_frameElement);
_wait.Until(d =>
{
var shippingInfoButton = d.FindElement(By.CssSelector("input[title ='Info']"));
return shippingInfoButton.Displayed && shippingInfoButton.Enabled;
});
var infoButton = _driver.FindElement(By.CssSelector("input[title ='Info']"));
ScrollToElement(infoButton);
infoButton.Click();
again this runs with no thrown exceptions so I'm assuming it has found the element and it is both displayed and enabled.
Let me know if you need any more info. Thanks
I can't explain why the selenium driver .click() method won't fire on some elements in the page but not others, but I did find a solution.
Using IJavaScriptExecutor you can click the element using javascript instead and in my case it worked.
Here is the code to run the IJavaScriptExecutor and below is my whole method.
//IJavaScriptExecutor
IJavaScriptExecutor js = _driver as IJavaScriptExecutor;
js.ExecuteScript("arguments[0].click();", infoButton);
//my whole method for clicking the button and returning the page object
public ShippingMethodDetailsPageObject SelectShippingMethodInfo()
{
_driver.SwitchTo().Frame(_frameElement);
_wait.Until(d =>
{
var shippingInfoButton = d.FindElement(By.CssSelector("input[title='Info']"));
return shippingInfoButton.Displayed && shippingInfoButton.Enabled;
});
var infoButton = _driver.FindElement(By.CssSelector("input[title ='Info']"));
IJavaScriptExecutor js = _driver as IJavaScriptExecutor;
js.ExecuteScript("arguments[0].click();", infoButton);
_driver.SwitchTo().DefaultContent();
return new ShippingMethodDetailsPageObject(_driver, false);
}
I ran into a similar problem. If it's the same problem there's a fault in the ChromeDriver it can't click certain elements because of surrounding divs etc. Bit lame really.
A simple fix is to send the Enter key e.g. element.SendKeys(Keys.Enter). Seems to work across all browsers.
I have some tests that works in Firefox all the times, and in Chrome it drove me mad, because sometimes it passed successfully, and sometimes the ".click" didn't work and it would fail the test.
Took a long time to notice it, but the reason was: I used to sometimes minimize the browser to 80% to be able to see the browser along side my IDE. it appears that the ".click" doesn't work when I did it.
At least for me this was the issue

Categories