Not Able to Locate NgModel - c#

driver.FindElement(By.XPath("//textarea[#ng-model='vm.system.systemdescription']")).SendKeys("abc"); //is able to find element.
Whereas if do same thing using ngDriver i'm getting
> async timeout exception or javascript Invalid operation exception
ngDriver.FindElement(NgBy.Model("vm.system.systemdescription")).SendKeys("jkgf");
Also tried the following but doesn't help
ngDriver.FindElement(NgByModel.Name("vm.system.systemdescription")).SendKeys("jkgf"); also does not work.
Here is d code snippet private IWebDriver driver = new InternetExplorerDriver();
private NgWebDriver ngDriver;
private WebDriverWait wait;
private Actions actions;
private int wait_seconds = 30;
[SetUp]
public void InitializeBrowserToSearchEngine()
{ ngDriver = new NgWebDriver(driver);
driver.Navigate().GoToUrl("req_url");
driver.Manage().Timeouts().SetScriptTimeout(TimeSpan.FromSeconds(60));
ngDriver = new NgWebDriver(driver);
wait = new WebDriverWait(driver, TimeSpan.FromSeconds(wait_seconds));
actions = new Actions(driver);
Console.WriteLine("Opened Browser with the given URL");
}
[Test]
[Order(6)]
public void OpenNewSystemConfig()
{
string url = "req_url";
ngDriver.Url = url;
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
WebDriverWait w1 = new WebDriverWait(ngDriver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.Id("Administration_1")));
driver.FindElement(By.Id("Administration_1")).Click();
wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.Id("System_Configuration_0")));
driver.FindElement(By.Id("System_Configuration_0")).Click();
wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.XPath("//textarea[#ng-model='vm.system.systemdescription']")));
Thread.Sleep(5000);
driver.FindElement(By.XPath("//textarea[#ng-model='vm.system.systemdescription']")).SendKeys("ijfk");
//w1.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(NgBy.Model("vm.system.systemdescription")));
//ngDriver.FindElement(NgBy.Model("vm.system.systemdescription")).SendKeys("jkgf"); ;
wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.XPath("//select[#data-ng-model='vm.system.SystemTypeId']")));
driver.FindElement(By.XPath("//select[#data-ng-model='vm.system.SystemTypeId']"));
//wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(NgBy.Model("vm.system.systemdescription")));
//ngDriver.FindElement(NgByModel.Name("vm.system.SystemTypeId"));
// ngDriver.FindElement(NgByModel.ClassName("form-control ng-pristine ng-untouched ng-valid ng-not-empty ng-valid-required"));
}
The commented code is not working. I need to use protractor to find the elements by ng-model and ng-binding. Since Xpath are not be used i need a way to find the element which does not have id and angular components could be find by the locators such as ng-model and ng-binding.
Also I have added protractor in the reference using nuget package manager
please help to resolve

just try element(by.model("vm.system.systemdescription")).sendKeys('jkgf);
You have a typo in your command sendKeys should start with small case 's' not 'S'
Hope this helps..

Related

How to apply wait for an element using WinAppDriver on UWP in C#

I am working on automation of UWP app using WinAppDriver with C# but at some points my tests are failing as some elements take time to appear and I couldn't apply waitDriverWait (guess WAD doesnt have this) so I found below code which is working but would like to know if there is any better approach please?
public static void WaitForElement(this WindowsDriver<WindowsElement> driver, string IDType, string elementName, int time = 10000)
{
var wait = new DefaultWait<WindowsDriver<WindowsElement>>(BasePage.WindowsDriver)
{
Timeout = TimeSpan.FromSeconds(time),
PollingInterval = TimeSpan.FromSeconds(0.5)
};
wait.IgnoreExceptionTypes(typeof(InvalidOperationException));
wait.Until(driver1 =>
{
int elementCount = 0;
switch (IDType)
{
case "id":
elementCount = driver1.FindElementsByAccessibilityId(elementName).Count;
break;
case "xpath":
elementCount = driver1.FindElementsByXPath(elementName).Count;
break;
case "name":
elementCount = driver1.FindElementsByName(elementName).Count;
break;
}
return elementCount > 0;
});
}
I have used WebDriverWait successfully with my WinAppDriver automation in the past -- you will have to install the DotNetSeleniumExtras.WaitHelpers to access the ExpectedConditions class as it is now obsolete in the standard Selenium namespace, but I don't see anything stopping you from using WebDriverWait.
Waiting for element Text to be populated with a value:
var Driver = new WindowsDriver<WindowsElement>();
var myElement = Driver.FindElementByAccessibilityId("someId");
var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(30));
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.TextToBePresentInElement(myElement, "text"));
Waiting for an element to be displayed:
var Driver = new WindowsDriver<WindowsElement>();
var myElement = Driver.FindElementByAccessibilityId("someId");
new WebDriverWait(driver, TimeSpan.FromSeconds(30)).Until(d => myElement.Displayed);
Waiting for an element to exist, using another approach with FindElements and By.Name:
var Driver = new WindowsDriver<WindowsElement>();
new WebDriverWait(driver, TimeSpan.FromSeconds(30)).Until(d => d.FindElements(By.Name("someName")).Count > 0);
As mentioned, I have used these exact methods for waiting in the past with WinAppDriver, with success. Please let me know what issues you encounter with these approaches.

How can I wait for an element from another element in C# Selenium

Is there a way to perform a wait.Until by somehow searching the element from another element, rather than from the whole driver?
example:
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSec));
IWebElement element = wait.Until(ExpectedConditions.ElementExists(by));
I cannot modify the 'by' to be too specific and when it is searched under the driver and gets the wrong element.
IWebElement element = wait.Until(drv => drv.FindElement(by));
this option also searches under the driver.
I want something like this:
public static IWebElement WaitElement(this IWebElement webElement, IWebDriver driver, By by, int timeoutInSec = 5)
{
try
{
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSec));
IWebElement element = wait.Until(webElement.FindElement(by));
}
When I try to write this code I get this error:
enter image description here
It may be late, but hopefully will help someone else. Just change the syntax of your last line in the 3rd option to the following:
IWebElement element = wait.Until(d => webElement.FindElement(by));

c# with Selenium: element not visible

I'm trying to create an automated ui test with selenium in c#. Here is my code:
driver = new ChromeDriver();
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));
driver.Navigate().GoToUrl("my_url");
driver.FindElementById("textBox").Clear();
driver.FindElementById("textBox").SendKeys("tire");
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.Until((ExpectedConditions.ElementIsVisible(By.Id("Moto"))));
driver.FindElementById("Moto").Click();
Before using a wait.until, I was getting the exception ElementNotVisibleException, but now I'm getting the exception WebDriverTimeoutException because the element with the id "Moto" is not visible.
Here is a screenshot of a part of the DOM:
So why the moto checkbox is not found or is not visible?
Try the below code (in Java) as it is working at my end for the same structure -
driver.findElement(By.xpath("//label[#for='Moto']")).click();
I don't know why it is causing problem to find <input> tag by id
You might need to scroll to the element to make it visible
IWebElement moto = driver.FindElement(By.Id("Moto"));
Actions actions = new Actions(driver);
actions.MoveToElement(moto).Perform();
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.Until(ExpectedConditions.ElementIsVisible(By.Id("Moto"))).Click();

.Text not working in Selenium PhantomJS C#

I want to get the text Sample from this structure:
<td id="IDName">Sample</td>
so I tried this:
driver1.FindElement(By.Id("IDName")).Text;
but it always return null.
Is there any reason why is it not working?
It's Hard to say why .Text is not working in your case, Might be possible when you are going to find element it's present on the DOM without text, So you should WebDriverWait to wait until element exists as below :-
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element = wait.Until(ExpectedConditions.ElementExists(By.Id("IDName")));
element.Text;
Or might be possible it's designing issues of your HTML, then you can get text by using .GetAttribute("innerHTML") as below :-
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element = wait.Until(ExpectedConditions.ElementExists(By.Id("IDName")));
element.GetAttribute("innerHTML");
Or then you can get text by using .GetAttribute("textContent") as below :-
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element = wait.Until(ExpectedConditions.ElementExists(By.Id("IDName")));
element.GetAttribute("textContent");
Hope it helps...:)

.net phantomjs + selenium unable to get element

All is working well when I was using Selenium alone, but when i tried with phantomjs i get null in finding elements.
static void Main()
{
IWebDriver driver = new PhantomJSDriver();
driver.Navigate().GoToUrl("https://sellercentral.amazon.de/gp/homepage.html");
var username = driver.FindElement(By.Id("username"));
var password = driver.FindElement(By.Id("password"));
username.SendKeys("*************************");
password.SendKeys("*************");
driver.FindElement(By.Id("sign-in-button")).Submit();
string messagesURL = "https://sellercentral.amazon.de/gp/communication-manager/inbox.html/ref=ag_cmin__cmin?ie=UTF8&clcmResponseTimeSuboptions=&dateExactEnd=&dateExactStart=&dateFilter=&itemsPerPage=20&marketplaceId=A1PA6795UKMFR9&otherPartyId=&pageNum=1&refIndex=40&searchBoxText=&showFilters=0&sortBy=ArrivalDate&sortOrder=Descending";
driver.Navigate().GoToUrl(messagesURL);
ParseMessages(driver);
}
public static void ParseMessages(IWebDriver driver) {
var node = driver.FindElements(By.ClassName("list-row-white"));
foreach (var n in node) {
var refNo = n.FindElement(By.ClassName("data-display-field-border-lbr"));
Console.WriteLine(mi.refNo);
}
}
In this line of code, i get null: var node = driver.FindElements(By.ClassName("list-row-white"));
But when i used selenium alone with actual browser, all is working. But i wanted to get things to be headless.
I am new to phantomJS, correct me if i implemented it correctly and if my code is right.
In some cases, PhantomJS has issues working with css related stuff or element classes.
In such case, converting locator to XPath may solve the problem.
// Thread.Sleep(3000) // Please, replace me with WebDriverWait ^_^
var node = driver.FindElements(By.XPath("//*[contains(#class,'list-row-white')]"));
Another point, PhantomJS works much faster than any other browser.
Try to insert Thread.Sleep before the failed code line.
If the code will pass -- please, replace it with proper WebDriverWait expression

Categories

Resources