I am new to Selenium C# automation. Tried finding on web but did not get any help.
The html code looks like this. I need to find the element and then click it using CSS. The site only runs on IE.
<tbody>
<tr class="t-state-selected">
<td>Purchased</td>
<td class="">768990192</td>
I know web links can disappear, but here are a few I use when trying to figure out how to locate elements using Selenium's C# WebDriver:
https://automatetheplanet.com/selenium-webdriver-locators-cheat-sheet/
https://saucelabs.com/resources/articles/selenium-tips-css-selectors
https://www.packtpub.com/mapt/book/web_development/9781849515740/1
The bottom line is that you're selecting by id, class, or XPath. Each of these can be tested directly on the page using the F12 browser tools. For example, to find the first comment on your question above, you could try this in the console:
$x("//div[#id='mainbar']//tbody[#class='js-comments-list']/tr")
Here's another SO post with a quick and dirty answer.
And here is the official documentation from Selenium on how to locate UI elements.
To click on the number 768990192 which is dynamic we have to construct a CssSelector as follows :
driver.FindElement(By.CssSelector("tr.t-state-selected td:nth-of-type(2)")).Click();
You're really not giving us much info to work. I will try my best to accommodate. Even though the presented HTML is not enough to give an indication of the format and you've not presented any code of your current solution.
string url = "https://www.google.com";
IWebDriver driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl(url);
driver.FindElement(By.XPath("//tr[#class='t-state-selected']")).Click();
This little code snippet.
Creates a internet explorer driver.
Goes to the url of your choice.
And then clicks the table row that has a class that equals "t-state-selected'. Which my guess is all or none of the table rows.
Related
I am trying to automate an web application using Selenium Webdriver and C#. Currently I am stuck with a simple issue. After navigating to a particular page, I am trying to click a link which has a link text of "Manage groups". However when I am executing the test it is failing to find the element in the web page.
driver.FindElement(By.LinkText("Manage groups"));
I also tried using absolute Xpath:
driver.FindElement(By.XPath("html/body/div[1]/div[1]/div[2]/div[2]/article/li[2]/a"));
In both the cases the test navigates to the page then fails with error:
unable to find the element
The issue may be caused by a small mistake in 'Manage groups' text (e.g. there may be an addition whitespace).
Can you please provide at least little part of html (you can get it by right-click on your 'Manage groups' link and selecting 'Inspect Element' option )
if not - try these ones:
By.XPath("//a[#contains(., 'Manage groups')]")
By.XPath("//a[#contains(., 'manage groups')]")
By.XPath("//a[#value='Manage groups')]")
By.partialLinkText("Manage groups")
It should work with that :)
IWebElement ClickNext = driver.FindElement(By.XPath("//a[.='Manage groups']"));
// We get the link to click on
ClickNext.Click();
Edit : Try that li instead of a
IWebElement ClickNext = driver.FindElement(By.XPath("//li[.='Manage groups']"));
// We get the link to click on
ClickNext.Click();
I am coding a web browser control application that will perform some specific operations in a website.
There are, however, a couple pages of tables in the website, where transition in between is implemented with a Java Script command. Here is what I get when I use inspect element over one of the transition buttons:
<a class="ctl00_cph1_mnuPager_1" href="javascript:__doPostBack('ctl00$cph1$mnuPager','b2')"> 2 </a>
Given that I have already have the page in my c# application as follows:
HTMLDOCUMENT BrowserContent = webBrowser1.Document;
Can you give me any tips about how to programmatically click on that page transition button(ie. call that script).
I tried:
BrowserContent.InvokeScript("javascript:__doPostBack('ctl00$cph1$mnuPager','b2')");
But it didn't help.
Thanks a lot guys!
Try this:
BrowserContent.InvokeScript("javascript:__doPostBack('ctl00$cph1$mnuPager','2')");
This will invoke the script inside the double quotes.
C# Visual Studio 2010
I am loading a complex html page into a webbrowser control. But, I don't have the ability to modify the webpage. I want to click a link on the page automatically from the windows form. But, the ID appears to be randomly generated each time the page is loaded (so I believe referencing the ID will not work).
This is the content of the a href link:
<a
id="u_lp_id_58547"
href="javascript:void(0)"
class="SGLeftPanelText" onclick="setStoreParams('cases;212', 212); window.leftpanel.onClick('cases_ss_733');return false; ">
My Assigned</a>
Is the anyway to click the link from C#?
Thanks!
UPDATE:
I feel like this is close but it is just not working:
HtmlElementCollection links = helpdeskWebBrowser.Document.Window.Frames["main_pending_events_frame"].Document.GetElementsByTagName("a");
MessageBox.Show(links.Count.ToString());
I have tried plugging in every single frame name and tried both "a" and "A" in the TagName field but just have not had any luck. I can just not find any links; the message box is always 0. What am I missing?
Something like this should work:
HtmlElement link = webBrowser.Document.GetElementByID("u_lp_id_58547")
link.InvokeMember("Click")
EDIT:
Since the IDs are generated randomly, another option may be to identify the links by their InnerText; along these lines.
HtmlElementCollection links = webBrowser.Document.GetElementsByTagName("A");
foreach (HtmlElement link in links)
{
if (link.InnerText.Equals("My Assigned"))
link.InvokeMember("Click");
}
UPDATE:
You can get the links within an IFrame using:
webBrowser.Document.Window.Frames["MyIFrame"].Document.GetElementsByTagName("A");
Perhaps you will have to isolate the link ID value using more of the surrounding HTML context as a "target" and then extract the new random ID.
In the past I have used the "HtmlAgilityPack" to easily parse "screen-scraped" HTML to isolate areas of interest within a page - this library seems to be easy to use and reliable.
For learning purposes I would like to automate some parts in a browser game, currently I am trying to fill out some simple text boxes, without any luck though. I've created a WebBrowser component on my form, loaded the website via it and tried this.
webBrowser1.Document.GetElementById("citizen_name").SetAttribute("", "myname");
When I click my "fill out text box" button nothing happens. The HTML part looks like this:
<input type="text" name="citizen_name" id="citizen_name" value="" class="field" tabindex="1" />
I am talking about the eRepublik.com game, appreciate any help.
Try this:
HtmlDocument document = this.webBrowser1.Document;
document.GetElementById("citizen_name").SetAttribute("value", "myname");
I usually take the following approach:
var someElem = webBrowser1.Document.GetElementById("some_id");
if (someElem != null)
{
someElem.InnerText = "Some value";
}
Some textareas of advanced editors cannot have their value set this way. To handle them I do something like the following:
someElem.Focus();
Windows.Forms.SendKeys.SendWait("Some value");
For this kind of problems there are much more suitable environments.
The easiest to use is definitely userscripts.
What exactly do you want to learn? How to test web apps, or how to develop them?
You can use of course simple javascript that you include in your page, or better yet, using Greasemonkey , so you don't modify the "client" code.
But greasemonkey would only be an option for Firefox, Opera and Chrome. If you really need a full blown cross browser automation test suite, you could use Selenium IDE , that allows to record or script a series of interactions with a web page, that can be automatically run in any of its supported browsers.
i want create small web browser , tiny and fast
but i have problem ,
let me explain :
1 - user enter site : google.com
2 - c# program get google.com
3 - find <td nowrap="" align="center">
4 - in web browser only show that area
i dont know where i must start ,
thanks
Ok, I'm going to try answer your question, but I am deciphering as well.
Create a WebBrowser control on your form. (2.0 is fine for what you need) and .Navigate("http://www.google.com");
Get the source code from the Document. You can do this as follows: string source = _WebBrowser.Document.Body.OuterHtml;
Use string manipulation to get to the area on the page you need. For instance .SubString() functions
Save the text into a file, or stream and load it into the WebBrowser control, or replace the pages Document HTML with just the HTML you are wanting to show.
Okay! Looking at the comment it seems you want to request for a page using c# and show only one part of the page. In your case its that specific <td> . Please correct me if I am wrong.
Other than what Kyle has mentioned. Check out HTML agility Pack. It might be of interest to you.