I need to click the button "Add" in the post new wordpress form, this button is to add tags to the post , the trouble is that button don't have the value and id propertie. Is just like that
the html for the button
input type="button" class="button tagadd" value="Add" tabindex="3"
my tries
webBrowser1.Document.GetElementById("button tagadd").InvokeMember("click");
webBrowser1.Document.GetElementById("Add").InvokeMember("click");
"GetElementById without id"
:-)
Unless you can change the markup for the button
What you need now is to traverse the entire DOM and look for a button in a known place. I'd suggest adding jquery if not already exist to be able for easier dom manipulation/search.
If you add jquery you could do something like $(".tagadd").click()
You could try doing
webBrowser1.document.getElementsByClassName("tagadd")
EDIT: Here is a script to create the getElementsByClassName function if it's not available http://robertnyman.com/2008/05/27/the-ultimate-getelementsbyclassname-anno-2008/
There is also this http://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.getelementsbytagname.aspx but I've never used it.
Add an ID. Even if you're dynamically generating the buttons this should be trivial.
If you're using jQuery,
$('.tagadd')
will return a collection of everything with the tagadd class applied. You can further filter this by the other classes (button, etc)
Use this:
onload=function(){
if (document.getElementsByClassName == undefined) {
document.getElementsByClassName = function(className)
{
var hasClassName = new RegExp("(?:^|\s)" + className + "(?:$|\s)");
var allElements = document.getElementsByTagName("*");
var results = [];
var element;
for (var i = 0; (element = allElements[i]) != null; i++) {
var elementClass = element.className;
if (elementClass
&& elementClass.indexOf(className) != -1
&& hasClassName.test(elementClass))
results.push(element);
}
return results;
}
}
}
and another
Some browsers provide the method getElementsByClassName() which lets you select by class without using jQuery (which is a bit heavy if this is all you need). I haven't tested this so I'm not sure how widely it's supported.
Did I mention that you should give everything an ID?
use TagName isteed for example
var elems = webBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement elem in elems)
{
if (elem.GetAttribute("class") == "button tagadd")
{
elem.InvokeMember("click");
}
}
Related
I am not using selenium nor anything else, i just want to do it on the webbrowser on the windows form application.
I have a windows form application and i want to click on a button with code but there is no ID.
I tried using a lot of different things found on this websites forums, but none of this works.
Have you tried using WebBrowser.GetElementByTagName("div") and then checking each element against attribute type=submit?
Your code should look something like
HtmlElement submit = FindSubmitElement(webBrowser1.Document);
submit?.InvokeMember("submit");
public HtmlElement FindSubmitElement(HtmlDocument document)
{
HtmlElementCollection elems = document.GetElementsByTagName("div"); // since your tag is div
// this will return collection, even in case there is just one div, find the first one, having an attribute 'type' with value 'submit'
foreach (HtmlElement elem in elems)
{
string type = elem.GetAttribute("type");
if (!string.IsNullOrEmpty(type) && type == "submit")
{
return elem; // if div tag with attribute type is found exit and return that html element
}
}
return null; // if no div tags found with an attribute 'type' return null
}
Check more on GetElementsByTagName method on the MSDN docs. Code is taken from there and adjusted to your need.
I want to programmatically click button on a webpage with source like this
<input alt="BusiBtn" class="aButtn" type="submit" value="Search" tabindex="16">
When I do
WebBrowser b = new WebBrowser();
b.Navigate(URL);
while (b.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
b.Document.GetElementByID("BusiBtn").InvokeMember("click");
I get "Object reference not set to an instance of object error".
Can somebody help.
Thanks
Rashmi
What you can do in this case simply find all the HtmlElements having input tag. If you need to invoke all the input tags in general, then just invoke click on them. And if you need only the above input element, then filter all the input tags to search for the specific tag with the attribute values like above. Please have a look at the following code:
HtmlElementCollection elems = b.Document.GetElementsByTagName("input");
foreach (HtmlElement elem in elems)
{
string altStr = elem.GetAttribute("alt");
string classStr = elem.GetAttribute("class");
string typeStr = elem.GetAttribute("type");
string valueStr = elem.GetAttribute("value");
string tabindexStr = elem.GetAttribute("tabindex");
if((altStr == "BusiBtn") && (classStr == "aButtn") && (typeStr == "submit") && (valueStr == "Search") && (tabindexStr == "16"))
{
elem.InvokeMember("click");
break;
}
}
You're using the wrong field.
alt is for alternative text.
You have not actually given that button an id of BusiBtn.
Try:
<input id="BusiBtn" class="aButtn" type="submit" value="Search" tabindex="16">
The clue is in the GetElementByID call. It's not called GetElementByAlt for a reason ;)
add 'name' property to input tag and then use GetElementsByName property
You should use the GetElementsByTagName method instead of GetElementById to get all Input-Elements on the page and then cycle through using GetAttribute. An example can be found here http://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.getelementsbytagname(v=vs.110).aspx.
I have a WebView on my app and I can't change the html file("target=_blank" link types). But some links on the page makes my app open them on the system browser. How can I disallow this action?
Thanks.
In the NavigationCompleted event handler run this script:
webView.InvokeScriptAsync("eval", new[]
{
#"(function()
{
var hyperlinks = document.getElementsByTagName('a');
for(var i = 0; i < hyperlinks.length; i++)
{
if(hyperlinks[i].getAttribute('target') != null)
{
hyperlinks[i].setAttribute('target', '_self');
}
}
})()"
});
On Windows 10, you can use WebView.NewWindowRequested:
private void WebView1_NewWindowRequested(
WebView sender,
WebViewNewWindowRequestedEventArgs args)
{
Debug.WriteLine(args.Uri);
args.Handled = true; // Prevent the browser from being launched.
}
There is a navigation starting event. It have a cancel property that can be used to cancel the navigation. Maybe this will work for you?
http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.webview.navigationstarting
Stumbled on this myself recently, and I want to add that even though user2269867's answer is a viable solution, it might not work in certain situations.
For example, system browser will not only open if user click a link with target="_blank" attribute, but also if window.open() function called in javascript. Moreover, even removing all 'target' attributes won't work if a page loading some content dynamically and changing DOM after your script is already finished executing.
To solve all problems above, you need to override window.open function and also check for 'target' attribute not once, but every time user click something. Here is script that covers those cases:
function selfOrParentHasAttribute(e, attributeName) {
var el = e.srcElement || e.target;
if (el.hasAttribute(attributeName)) {
return el;
}
else {
while (el = el.parentNode) {
if (el.hasAttribute(attributeName)) {
return el;
}
}
}
return false;
}
var targetAttributeName = "target";
document.addEventListener("click", function (e) {
var el = selfOrParentHasAttribute(e, targetAttributeName);
if (el) {
if ((el.getAttribute(targetAttributeName) == "_blank") ||
(el.getAttribute(targetAttributeName) == "_new"))
{
el.removeAttribute(targetAttributeName);
}
}
});
window.open = function () {
return function (url) {
window.location.href = url;
};
}(window.open);
My js skills aren't ideal, so feel free to modify.
Also don't forget that, as kiewic mentioned, for Windows 10 there is WebView.NewWindowRequested event which solves this issue more natural.
If you just want to show the page and not allow any action to be done on that page I would look into WebViewBrush. The WebViewBrush will basically screenshot the website and the users will not be able to use any links or anything else on that page, it will turn into a read-only page. I believe this is what you are asking for.
More info on WebViewBrush can be found here: http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.webviewbrush
If you can edit HTML of the page and NavigateToString(), then add <base target='_blank'/> in the <head>
I have written code for javascript but it is not called any how. I tried to call both form html side and also by assigning attribute from page load event but it is not at all called.
This is the code for my javascript.
function rdbantiplatelet_onClick(thiscontrol, trName) {
alert('hi');
var RB1 = thiscontrol;
var radio = RB1.getElementsByTagName("input");
var trDose = document.getElementById(trName.toString());
// var RB1 = document.getElementById("<%=this.rdbantiplatelet.ClientID%>");
// var radio = RB1.getElementsByTagName("input");
// var tblAntiplatelet = document.getElementById("<%=tblAntiplatelet.ClientID %>");
for (var i = 0; i < radio.length; i++){
if (radio[i].checked){
trDose.style.display = "";
return true;
}
else{
trDose.style.display = "none";
return true;
}
}
return false;
}
This is the code to call javascript written in page_load event..
rdbantiplatelet.Attributes.Add("OnClick", "return rdbantiplatelet_onClick(this,'" + trDose.ClientID.ToString() + "');");
Try an alert() first to make sure your onclick is firing, then try your rdbantiplatelet_onClick() function:
rdbantiplatelet.Attributes.Add("OnClick", "alert('I am working');");
First of all for your reference here is a list of standard html events
http://www.w3schools.com/tags/ref_eventattributes.asp
try adding something like this to the html radiobutton element
onchange="alert('on change fired');"
and
onclick="alert(' on click fired');
so you can make sure you are picking up the right event.
Once you have the right event then replace the alert call to your method
which would be something like this
onchange="rdbantiplatelet_onClick(this, this.parent.id)"
...you might have to change the 'this.parent.id'
Make sure you are running through a good web browser for development FireFox with the firebug plug-in is great for this stuff.
You are add click event to html table that holds radiobuttons. Use script below:
foreach (ListItem item in rdbantiplatelet.Items)
{
item.Attributes.Add("onclick", "return foobar(this);");
}
If I have a textbox txtInfo in a form that I submit via post, if I post back to the page I can read the value entered in the textbox using txtInfo.Text. What if I am posting to a different page? Do I have to parse Request.Form for the control name mutilations (which is what I am doing now) or can I get it from that mess .net passes around as state?
Thanks
Thank you for the answers so far... Sorry I should have been a little more clear. This control is a runat="server" control. This is what I am relegated to now - not very pretty.
foreach (String key in page.Request.Form.AllKeys)
{
String[] controlName = key.Split('$');//remove that horrrible .net naming - thanks Bill.
keyName = controlName[controlName.Length - 1];//get the last value so we always have the name
keyValue = page.Request.Form[key];
if (keyValue != "")
{
switch (keyName)...
You should look into Cross Page Postbacks.
As noted on this page you can easily get access to txtInfo using the following:
if (Page.PreviousPage != null)
{
TextBox SourceTextBox =
(TextBox)Page.PreviousPage.FindControl("txtInfo");
if (SourceTextBox != null)
{
Label1.Text = SourceTextBox.Text;
}
}
What's wrong with...
string txtInfo = Request.Form["txtInfo"];
if(txtInfo == null) txtInfo = "";
A simple solution would be use a simple <input type="text"> instead of an <asp:TextBox>. Give it a name attribute and then access it via Request.Form.
.aspx file:
<input type="text" name="foo" />
Posted-to code-behind (same page, different page, doesn't matter):
var text = Request.Form["foo"];