I want to click a website submit button using my c# window application
<p><input type="submit" value="Send" class="wpcf7-form-control wpcf7-submit"/></p></div>
https://www.devpundit.com/contact/ - check this page to see the submit button
It seems like it is calling ajax or javascript call to submit the button.
now how to do this.
I have tried the below 2 option but not working
webBrowser2.Document.GetElementById("wpcf7-submit").InvokeMember("click");
HTMLInputElement submit = (HTMLInputElement)pass.all.item("wpcf7-form-control wpcf7-submit", 0);
First - you are telling the computer to grab the button by its ID, but instead of using the ID, you are putting the class-name where the ID goes. I inspected the element on that page and did not see an "ID" for that element. So let's use what the website gave us.
https://www.w3schools.com/jsref/met_document_getelementsbyclassname.asp
This will give you a list of all the elements that share that class. So instead of
webBrowser2.Document.GetElementById("wpcf7-submit").InvokeMember("click");
please try
Edit: So it turns out there is no GetElementsByClassName in the C# WebBrowser. Another approach instead would be recognizing that the button has a value of "send". The "value" is an attribute, and can be accessed in C#.
You can run a for-loop through the elements in the page and check which input elements have the value "send". It's a bit crude, but it should work.
//Create list of HtmlElement objects
//Populate list with all elements with tag name 'input'
HtmlElementCollection elementList = webBrowser2.Document.GetElementsByTagName("input");
//loop through the items in elementList, match the one with value set to 'send'
foreach (HtmlElement currentElement in elementList)
{
if (currentElement.GetAttribute("value") == "Send")
{
currentElement.InvokeMember("click");
}
}
Related
hi i am applying css to one of my list elements which is inside a tag which is in a master page. so when i click this link the style is getting applied but the page reloads and then the applied style is again reset to default this is my code can somebody help me with this? please?
function SelectThis(ctrl) {
debugger;
var list = document.getElementById("myslidemenu").getElementsByTagName('a');
for (i = 0; i < list.length; i++) {
list[i].style.color = "white";
list[i].style.background = "#414141";
}
ctrl.style.background = "black";
ctrl.style.color = 'yellow';
}
but since i am clicking on a hyper link. the master page reloads and the selected styles are lost
Since it is a hyper link is residing inside the <li> use
e.preventDefault(); will stop the default action of the <a> tag
Update:
list[i].addEventListener('click',preventReload);
function preventReload(e){
e.preventDefault();
}
Add this code inside the for loop, which will stop the default functionality of the hyperlink i.e., stops the page from reloading.
If you need the page to reload, because it's a menu to sub pages then the way I do this is to:
Define a class for the style
Compare the current page url to the a href url (various ways to do this and various levels of matching)
If they match apply the class to that element
I would do this server side, not with javascript.
Another option is to use a cookie/session variable, not sure on the C# syntax, I did this recently in PHP and used AJAX to set a session variable after filtering some data to remember the current state when clicking down to the next level.
I have a set of common methods which I run for each test. Like I need to add a few items in my cart and the whole test runs for each item. But for the first item it runs fine and when the process is repeated for the second item it fails while verifying a text and I get a StaleElementReferenceException.
How do I look up the item again or solve this ? Thanks.
Code where it fails:
public bool VerifyItemPresentInCart()
{
//Get the cartsize and verify if one item present
IWebElement cartSize = driver.FindElement(By.CssSelector("div[class='cart-size']>div"));
string actualMsg = cartSize.Text;
string expectedMsg = "1";
VerifyIfTextPresentMethod(expectedMsg,actualMsg);
return true;
}
Error at
IWebElement cartSize = driver.FindElement(By.CssSelector("div[class='cart-size']>div"));
Update: html code
<a class="header-button show-cart has-cart-items" data-view-name="cart-badge" data-view-cid="view5" data-model-cid="c6" data-tappable="true">
Cart
<div class="cart-size">
<div>3</div>
</div>
New code:
IWebElement cardDetails = driver.FindElement(By.CssSelector("div[class='form-field clear-fix']>label[for='cardNumber']>div"));
I would try joining your lines of code:
IWebElement cartSize = driver.FindElement(By.CssSelector("div[class='cart-size']>div"));
string actualMsg = cartSize.Text;
So that they are:
string actualMsg = driver.FindElement(By.CssSelector("div[class='cart-size']>div")).Text;
This means that the text will be retrieved on the element as it is selected. I am wondering if between getting a handle on the element via its parent and retrieving the text you are losing focus on that element. Alternatively, remove the >div from your css and see if it retrieves the text anyway.
This not working suggests the situation you are facing is the point under the title The Element is not Attached to the DOM. The fact that your target text is within only divs suggests that this area is being styled by javascript and thus may only be active at certain times. If this element is not active, but is accessible, you can still receive the StaleElementReferenceException, as indicated at that page. Your next step would be to see if you can click a parenting div to activate this target div prior to accessing its text (eg. make sure the element is attached to the DOM then call the code you have provided).
For the last two weeks I have been kind of stuck on a problem.
I am developing some web scrapers using C# and I am using a WinForms WebBrowser control in my application. I am able to fill up the web form which is opened in my browser and submit it automatically by using the following code:
HtmlElement submitButton = document.GetElementById("Element_ID″);
submitButton.InvokeMember(“click”);
So far everything is fine, but the problem is that there is one another element in the web form that I want to click too, but this element does not have any id or name so I don't know how to click this one.
Please help me as soon as possible I need it for my master thesis.
(I want to click the next page arrow button in the give website:
http://www.gelbeseiten.de/yp/11//subscriberlist_pageAction.yp?sessionDataString=H4sIAAAAAAAAAI2PQU8CMRCFfw0XSEmns9128k5KongwGjFeSZftIqILbhcVf70NSgg3X-pbyXjLfvCFpqsbbIMpwbVRRuaBELKm6iew5T4gLFUpdmKpewJAGD8xV7JaxalfpdZX6mP31bH4WQfZblJehXcd2tGvr0WwbunVIKbYIZjjKmoa3atct4RSh-pA/S912oY4qhWzyjJkLvPZV4P4JetNFHYWOG2OoCH4pZlyU-pjWdhjS/LY2sp7-p1lLCLOGXwTLqpT1XSqOiXcpE3Xzw-pncUtGSDNp0ZZwR0we92TxSHjIX0x-pIQM-p0AZuciLl7M/kGE-pmcGjIOsvEpTB-pADJS0suGAQAA&page=0&filterTrade=-&filterFunction=-&sortBy=sort_trade&availableLetters=ABCDEFGHIJKLMNOPQRSTUVW )
I've written many web-scrapers in the past using embedded WebBrowsers, so you've come to the right place.
When the element does not have a name you need to find it by either content, or another associated element that is named.
In the first instance we wrote helper methods to iterate the hierachy looking for a specific piece of content within an element.
For the second option you get the named element and use a specific index for the desired child.
A combination of both (find a specific parent then look for a child with the right content)
In your specific example webpage, the next page anchor has a class type of "arrow next" you can search for.
You could do
HtmlElement next_arrow = document.GetElementsByTagName("a")
.Cast<HtmlElement>()
.Where(e => e.GetAttribute("class") == "arrow next")
.FirstOrDefault();
if (next_arrow != null)
{
next_arrow.InvokeMember("click");
}
Here's a trick, not by InvokeMember("click") rather just "simulating the click" -
this is the link for the first page:
gelbeseiten.de/yp/11//subscriberlist_pageAction.yp?sessionDataString=H4sIAAAAAAAAAI2PQU8CMRCFfw0XSEmns9128k5KongwGjFeSZftIqILbhcVf70NSgg3X-pbyXjLfvCFpqsbbIMpwbVRRuaBELKm6iew5T4gLFUpdmKpewJAGD8xV7JaxalfpdZX6mP31bH4WQfZblJehXcd2tGvr0WwbunVIKbYIZjjKmoa3atct4RSh-pA/S912oY4qhWzyjJkLvPZV4P4JetNFHYWOG2OoCH4pZlyU-pjWdhjS/LY2sp7-p1lLCLOGXwTLqpT1XSqOiXcpE3Xzw-pncUtGSDNp0ZZwR0we92TxSHjIX0x-pIQM-p0AZuciLl7M/kGE-pmcGjIOsvEpTB-pADJS0suGAQAA&page=0&filterTrade=-&filterFunction=-&sortBy=sort_trade&availableLetters=ABCDEFGHIJKLMNOPQRSTUVW
as you see page=0; clicking next, gives the link -
gelbeseiten.de/yp/11//subscriberlist_pageAction.yp?sessionDataString=H4sIAAAAAAAAAI2PQU/DMAyFf00vmzLFdprE8gkmwTggEENcp3RNxxh0o-pmA8euJBlO1G0-p-pvCf58zNwUzW-pDKyQalSmckExl6DqJpKnPCEuVbDaYFUvBcEIFXgVu1Ws2nV6Xac-pZn89X5xFwoed2MvQbmI73rf1eL4L3SakFFsJOBpnzcJbte9W4hSI-pQ/S912oY4qhWz5LDSC992Dl/QR60ahPki2OZKeNfCgiba18oicmLV8lTcoS8t6BJ8zsHMo3yEU1VE1D1ZmWm7Tt-psXxtNwCMmjS4BhJ7oDAy72WR5CH/MT0l1HQEVa46QDK2Z/JsTyhcdIAWrZeGy8/k7LJ5YQBAAA-e&page=1&filterTrade=-&filterFunction=-&sortBy=sort_trade&availableLetters=ABCDEFGHIJKLMNOPQRSTUVW
now page=1
and so on... in general clicking next means page=(x+1) clicking prev means page=(x-1). so build a string according the requirements. this addresses ur problem, however there are some other data also sent with querystring, that u have to append to the string as well.
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.
I have a facebook game application that is released at facebook platform at the following link :
FaceBookGameApplication
and i am using iframe and the problem now that after answering the question in the game i reload the page from javascript code :
window.location.href = sURL;
and on posting back i am updatting 2 divs of the score and hits with new values by getting the value exists and incrementing it with one according to user answer as the game is some questions and user answers them :
Hits = int.Parse(HitsNo.InnerText) + 1;
HitsNo.InnerText = Hits.ToString();
or :
Hits = int.Parse(HitsNo.InnerHTML) + 1;
HitsNo.InnerHTML = Hits.ToString();
and in the first time of reloading the values are changed and after that on each time of the reload the values don't change and the 2 divs don't take the new values that i set to them. The 2 divs runat server and i am using C# under .Net 2008 so what cause the two divs not changing thier innerText or innerHTML with new valuies io set from codebehind?
thanks in advance
If I'm following correctly, you're using HTML divs to display the score. Then from code-behind, you're retrieving the HTML from those divs to access the most recent score value.
The problem then is that the DIVs are not part of the http post. Therefore each time your code-behind runs it is using the original value, adding one to it, and then displaying the new value. The next time the postback occurs, again it uses the original value, adds one, and displays.
Instead, use a hidden input element to store the score, as that will be part of the http post each round trip.
For example:
<input type="hidden" id="HitsNo" value="1" runat="server" />
Then you can access that hidden value in your code-behind.