C# Selenium Element Finding problem using xpath - c#

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.

Related

Selenium: Finding if a Webelement is contained inside another Webelement?

I'm currently learning Selenium by building a test framework up to test Trello - for no good reason other than it's free and reasonably complex.
The problem I have right now is that, I want to check if a card is in a certain column or not.
I have the card & column as WebElements so what I'm doing right now is:
column.FindElements(By.LinkText(_card.Text)).Count > 0;
But this doesn't work if there's no text and seems pretty brittle.
What I want is something like:
column.Contains(_card)
I've searched on SO but I've only seen solutions which pass an XPath - I don't have an XPath, I have the WebElement.
Any ideas?
Two things,
relative xpath is fairly easy to learn and could probably take care of this for you.
CSS selectors should also easily identify the container regardless of the text. Without seeing the code, I can't help much more.
You should be able to find all elements with a certain css tag.
Using Firefox with the Firebug extension, right click your element and go to Inspect Element with Firebug. Then, when the html of your element comes up in the window, right click the element and select Copy XPath. Now you have an XPath to use.
To use the CSS Selectors that others are talking about, you can select Copy CSS Path instead of Copy XPath.
Hope this helps.

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?

How do I select individual words inside a Google document using selenium webdriver (C# or Java)

I'm currently testing content within google documents using selenium webdriver. Some of my tests involve selecting individual words within a google document then performing some action against them such as bold the word or change the font type for the specific word etc.
I would simply like to be able to select a word like this:
http://s10.postimg.org/9x3d4f1q1/image.png
And here is the code returned from the Google document:
http://s24.postimg.org/e4zfocy9x/image.png
I have tried using send keys to send a ctrl+a command and this works for me but the problem is, I need to do a little house keeping prior to running my test by creating a document with one word inside it. Kind of defeats the purpose of automating this.
I have tried using substring to get specific words but then I can't perform any action on the String as it will not be a web element.
Would someone be so kind and point me in the right direction? Thanks very much for any help. It is much appreciated.
Selenium can only manipulate WebElement
In your example you won't be able to manipulate only "is" which is a text, not a HTML node.
The best you can do is selecting the <span>:
driver.findElement(By.xpath("//span[contains(text(),'This is a paragraph')]"));
and do whatever you want with it

Selenium WebDriver selecting from scope (IWebElement)

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.

Finding element using Selenium Web Driver in C#

So I've been working learning how to use Selenium in C# to do some automated testing for a project. However, I've hit a roadblock on this one. I have been trying to figure out of way to click the following link on this webpage.
Here is what I'm trying to target:
<A class='PortalLink' HREF="https://mywebsite.com/myprograms/launchprogram.jsp?" onClick="setUser('login','password');"><span>MyProgram</span></A>
Searching by ClassName hasn't turned up anything. Although there are multiples, I just wanted to see if I could detect the presence of them.
By.ClassName("PortalLink")
I tried a href based search using CssSelector, but this failed as well.
By.CssSelector("[href*='https://mywebsite.com/myprograms/launchprogram.jsp?']")
Lastly, I tried to use XPath and search by class and span content, but this failed to find the link as well.
By.XPath("//A[contains(#class,'PortalLink') and span[text()='MyProgram']]")))
The webpage in question contains 2 frames which I've tried both.
I'm waiting 200 seconds before timing out. What am I doing incorrectly? Thanks in advance for any help!
Assuming that this element is not appended to the DOM during ajax, your statement should be
By.CssSelector("a.PortalLink[href*='launchprogram.jsp']")
If there are multiple of these links, then we'll need to go further up in the parent-child hierarchy since this link has no more attributes that make this link unique.
If you can post the parent html of this link then we can suggest more options,
Can you try these......
//span[contains(text(),'MyProgram']
//span[contains(text(),'MyProgram']/../

Categories

Resources