WebBrowser filling in values by name instead of ID - c#

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.

Related

Unable to retrieve the values in a website using Selenium Web Driver on C#

When practicing wring a simple Unit Test on Selenium (Web Driver) on C#, I've faced one issue related to retrieve the values in the text box. The issue seems to be very basic but I tried different ways to solve it but no luck.
The simple website has URL http://www.donethedeal.com. After clicking on ADD TO CART and then, VIEW CART, there is a field named Quantity. To retrieve the value in this field, my code written in C# is as below:
Assert.AreEqual("1", driver.FindElement(By.XPath("//*[#id='content']/div/div/div/div/form/table/tbody/tr[1]/td[5]/div/input")).Text);
I can't figure why this doesn't work. It always shows unable to locate the element. The Xpath I input in this code line is retrieved by right-click on Quantity box > Inspect > highlight the text and copy Xpath on Chrome.
//*[#id="content"]/div/div/div/div/form/table/tbody/tr[1]/td[5]/div/input
You need to get the value attribute using GetAttribute, the "1" is stored there
Assert.AreEqual("1", driver.FindElement(By.XPath("//*[#id='content']/div/div/div/div/form/table/tbody/tr[1]/td[5]/div/input")).GetAttribute("value"));

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']/../

Post data to server and then parse the HTML-code C#

I'm trying to parse a website. The only problem is that the site dosen't use a specific URL to the site I wan't to parse. The content is being displayed to the site using JavaScript on the same webpage so the content is different depending on the searchquery.
Is it possible to choose a value from a dropdown-menu and then post that to the server and then parse the HTML-code in C#?
Clarification:The code is returned in HTML.
I know the name of the option from the dropdown i want to post, but how do I do that from code-behind?
Most sites do not really generate HTML in Javascript. Much more often you see Asp.Net sites where Javascript is used for a postback (and name of the dropdown is posted back in __EVENTTARGET field)
Then you can do the same in your application - you have to imitate filling the form - pass all the fields to the server including VIEWSTATE and EVENTTARGET.
Having said that, it might be against the site's terms of use.
You definitely need to checkout Selenium, it does exactly what you need. It is commonly used as a testing framework. However you can use it to manipulate HTML tags even when the website uses javascript.
Note: Selenium allows you to open and manipulate a website using a browser such as FireFox, Chrome, IE, etc. However, I think what you need here is to use the WebDriver, which manipulates the website without opening a browser. Most of my experience using Selenium is with Java, but I found multiple tutorials online for .net too.

Categories

Resources