Selenium WebDriver selecting from scope (IWebElement) - c#

Is there a way to select elements inside already obtained IWebElement.
Im trying to do this via C# binding (RemoteWebDriver) but
findElem.FindElements(By.CssSelector(someStmt))
returns all elements from page (not from inside findElem)

That is exactly the behavior of IWebElement.FindElement and IWebElement.FindElements. For most locators, including By.CssSelector, this works exactly as expected. The one exception is that using By.XPath, you need to prepend a "." to scope the XPath search to the current element. The WebDriver project's integration tests include tests for exactly this functionality. Without further context, like some example HTML that demonstrates the issue, further diagnosis is impossible.

Related

Selenium cant access any elements on the webpage

I'm writing Selenium tests in C# with Specflow and Nunit for a new project i'm working on and nothing inside the body can be selected anywhere at all
I can access body with
WaitUntil(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.CssSelector("body")));
but can not of the following work and throe element not found exceptions:
WaitUntil(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.CssSelector("halo-root")));
WaitUntil(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.CssSelector("halo-entry")));`
WaitUntil(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.CssSelector("halo-navigation-container")));
etc
error: no such element: Unable to locate element: {"method":"css selector","selector":"halo-root"}
There are no iframes
I have tried long waits to ensure its nothing asynchronous
the website is angular.js (I have tried some protractor approaches but nothing successfully)
I've also played around wit ElementIsVisible, ElementIsClickable etc
Ive tried different selectors (id, className, xpath etc)
All help appreciated
p.s cant share the entire SUT as it required vpn access.
First check your html. i think body tag must be present in iframe.
If it is present in iframe first switch to iframe then try any commands(wait or actions) on element.
The issue was when logging in through a portal (Jumpcloud) a new tab was opened (that I didn't notice) so the solution was just switch to that new window with _driver.SwitchTo().Window(_driver.WindowHandles[1])
Thanks for all the help though! :)

C# Selenium Element Finding problem using xpath

Here's the code, I am trying to locate the first email input box.
But I can't locate using the code below.
driver.Navigate().GoToUrl("https://www.nike.com/register");
driver.FindElement(By.XPath("/html/body/div[2]/div[3]/div[4]/form/div[1]/input"));
I've found the solution to this.
In chrome, when you are trying to inspect the elements of the page, the XPath you copied from the element is based on the first indicator. In this example, it is Id.
So I tried to use the Full XPath to locate the input box and it apparently doesn't work with C# selenium. (There could be other ways around this.)
(I used to write selenium with python and it does work when you are using Full XPath.)
In this case, I ended up solving the problem by using the second indicator of the XPath. (type = "email") Just make sure it is unique to the element you are trying to locate you should be fine.

Selenium webdriver C# - Unable to find the element in a grid developed using angular UI

I am trying to automate a web application developed using angular JS through selenium webdriver(C#) and in that i am trying to click on a cell in a angular UI grid, i tried finding by css selector or xpath but it didn't help.
Css selector is generating dynamic ID - #\31 460691734316-0-uiGrid-00KQ-cell > div
Xpath is also dynamic //*[#id="1460691734316-0-uiGrid-00KQ-cell"]/div
and i tried using
driver.FindElements(By.CssSelector("*[id^='1460'][id$='cell']"));
but it didn't help
any help will be highly appreciated. I can send more details if needed
For my particular problem with the HTML page containing iframes and developed with AnglularJS the following trick saved me a lot of time: In the DOM I clearly saw that there is an iframe which wraps all the content. So following code supposed to work:
driver.switchTo().frame(0);
waitUntilVisibleByXPath("//h2[contains(text(), 'Creative chooser')]");
But it was not working and told me something like "Cannot switch to frame. Window was closed". Then I modified the code to:
driver.switchTo().defaultContent();
driver.switchTo().frame(0);
waitUntilVisibleByXPath("//h2[contains(text(), 'Creative chooser')]");
After this everything went smoothly. So evidently Angular was mangling something with iframes and just after loading the page when you expect that driver is focused on default content it was focused by some already removed by Angular frame. Hope this may help some of you.
What about trying to find the element with Selenium IDE which is a plugin of firefox ?
In the IDE, you can easily find the selector using selecting the element with GUI
Rather than identifying the element specifically by its ID, could you use the elements around it instead? Is this cell within a table and at a consistent position? Is there a parent element you could more consistently select and iterate through the children in your C# program to identify the appropriate cell you're looking for?

Assert Javascript value in C# with Selenium

I'm writing a test for a webapp. At one point in the application the webpage is completed using Javascript.
For testing I'm using Visual Studio 2012 with NUnit and Selenium.
I want to check if the box with id=j_idt13:JNumber has the text value of sometext.
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
string valoare = (string)js.ExecuteScript("return $('#j_idt13\\:JNumber').val();");
Assert.IsTrue(valoare.Equals("sometext"));
I keep getting this error:
"Syntax error, unrecognized expression: unsupported pseudo:JNumber".
What am I missing here?
I know you have something that works but I'd like to caution you to avoid using JavaScript to fetch the value of the element, in fact in general it should be avoided when doing your tests except when there is no other way to do what you want to do. The reason is that Selenium is supposed to behave as a typical user would. Typical users don't type JavaScript into a page or interact with it directly. This goes extra for using jQuery as your tests should not assume that jQuery exists on the page and is functioning. Selenium itself provides the ability to fetch the values of fields so I'd recommend you rewrite your code to something like:
driver.FindElement(By.Id("j_idt13:JNumber")).GetAttribute("value");
After some trial and error I found something that works:
string valoare = (string)js.ExecuteScript("return document.getElementById('j_idt13\\:JNumber').value;");
Why it works, I don't know. Basically is the same command.
And jQuery is working with other commands, just not the one I tried first.

WebBrowser filling in values by name instead of ID

I'm working on a form filling software, and I was wondering how would I go about selecting a field via name attribute or even CSS selector/XPath (like in selenium) via a web browser? Instead of findbyid?
Or Selenium RC is my only option?
You can use XPath to select your elements.
Examples are on this page: http://wiki.openqa.org/display/SEL/Help+With+XPath
Selenium supports many different locator types. Use the "xpath=" prefix for XPath locators, except where "xpath" is already in the method name.
//form//input[#name="myFormFieldName"]
EDIT
To test on http://google.com
Command: type
Target: //form//input[#name="q"]
Value: TEST
After running this command, the input box on google.com should have the word TEST written on it.
Selenium isn't your only answer, but if you're trying to automate a web site, it's one of the few answers. A web browser alone won't do the job.

Categories

Resources