So basically I want to press a button in a website and it doesn't work. The HTML line looks like this:
<div id="btn-login">
<button type="submit">
<span>SIGN IN THROUGH STEAM</span>
</button>
</div>
And, the code line looks like this:
webBrowser1.Document.GetElementById("btn-login").InvokeMember("click");
When I press the button nothing happens.
The website i'm using is http://csgowitch.com
It looks like the click is currently on the div around the button, not on the button itself. Since the button is the first child of the div, try this:
webBrowser1.Document.GetElementById("btn-login").FirstChild.InvokeMember("click");
Related
When selenium tries to click a button, web site deleting a div above, and selenium clicks the element under the element that I wanted to click.
I try to click with XPath, CSS selector, and the class name and none of them work. Also, I tried action chains to click but not worked.
And before clicking, I can confirm the selected element is the right element.
var element=driver.FindElements(By.ClassName("warning"))[0];
Debug.WriteLine(element.Text);
This writes the right element to the output.
-Here is the code By.ClassName:
driver.FindElements(By.ClassName("warning"))[0].Click();
The element under the right element does not have a "warning" class but it's still clicking on that element.
The page is basically like this:
<div id="maindiv">
<div class="the div that will delete"></div>
<button class="warning"></button>
<button class="second"></button>
<button class="third"></button>
<button class="fourth"></button>
</div>
here is how its looks
Basically, I'm trying to click the "warning" button but the "third" button receiving the click.
What should I do?
I solved the problem. First I deleted the div before clicking, then I clicked the button.
Here is the code:
var element = driver.FindElement(By.XPath("//[#id=\"app\"]/div[7]/main/div/div/div[2]/div/div/div[1]/div[1]"));
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].parentNode.removeChild(arguments[0]);",element );
var button=driver.FindElement(By.XPath("//*[#id=\"app\"]/div[7]/main/div/div/div[2]/div/div/div[1]/span[1]/span/button"));
((IJavaScriptExecutor) driver).ExecuteScript("arguments[0].click();",button);
`
I have an ASP.Net form which looks like the following when rendered (many lines of markup removed for brevity):
<form onsubmit="document.body.style.cursor='wait';showLoader();" action="mypage.aspx">
<!-- form elements and controls -->
<input type="submit" />
</form>
In mypage.aspx.cs's Page_Load I am doing:
if(Page.IsPostBack)
{
Foo();
} else {
Bar();
}
Quux();
Without modifying the HTML page using the Chrome Dev Tools, if I submit the form it executes Bar(). But If I removed or replaced the onsubmit attribute in the form with my own, ASP.Net thinks that the page is doing a post back and executes Foo().
I find this behavior weird because the inline JS code in onsubmit does not change anything related to the form submission. Its function is only to add a loading GIF while the form is reloading. Could there be something I am missing here?
Instead of adding a submit button, try adding a regular button, and call your gif functionality from it:
<input type="button" onclick="document.body.style.cursor='wait';showLoader();" value="Click Me" />
This will not post anything, which is what you want. <input type='submit'> will always cause a postback, unless you cancel it with JavaScript.
JSFiddle: http://jsfiddle.net/0gd8t7ew/
I am testing a website using Watin in the page there is dropdown menu button like if I find that button and click it clicks the entire button and selects the first Option in the menu but I want to click the V of the button to show the dropdown menu(div) and select the option from it the code I had tried to click the button
ff.Button(Find.By("aria-describedby", "x-auto-456")).FireEvent("onmouseover");
ff.Button(Find.By("aria-describedby", "x-auto-456")).FireEvent(" Onmousedown");
ff.Button(Find.By("aria-describedby", "x-auto-456")).Click();
the Button HTML
<button class="x-btn-text" style="position: relative; width: 16px;" type="button" tabindex="0" aria-describedby="x-auto-456">
Edit:
I had tried to find the co-ordinates of the Image in the button and then click it with the below code but its slightly not working kindly any one point me how to click the button and show the div Instead of selecting a option automatically,
NameValueCollection eventProps = new NameValueCollection();
eventProps.Add("clientY", "240");
eventProps.Add("clientX", "240");
ff.Button(Find.By("aria-describedby", "x-auto-456")).FireEvent("onClick",eventProps);
You are not finding the button correctly:
Button button = ff.Button(Find.ByClass("x-auto-457"));
You need to find the button by its class name. Be aware that will find the first <button> or <input type="button|submit|reset"> element with that class name. If you still aren't getting the correct button, check to see how many buttons on the page have x-auto-457 for a class attribute value.
How can I create in C# a page to response to another one? So when you press the button,login form opens in a new widow(browser tab or widow), and as you login... the form automaticaly refresh the first page.
Like the Open ID form. You press the (Connect with Facebook) button it opens a new window with the login form and then it refreshes the the website where u pressed the button.
Sorry for my English!! :) & please help!
You open a new window from a button click like this:
Markup:
<asp:button type="button" id="btnLogin" runat="server" Text="Click me" OnClientClick="javascript:window.open('newPage.aspx'); OnClick="ServerSideCode" />
Or with a regular HTML button:
<input type="button" id="btnLogin" value="Click me" onclick="javascript:window.open('newPage.aspx');" />
On newPage.aspx you define a function to auto close the current form and reload the parent form. Something like:
function closeMe()
{
window.parent.location.reload();
self.close();
}
And on server-side, when you handle your button Click event, you add this line all the way at the bottom depending on whether the process was successful. Example:
if(loginSuccessful)
ScriptManager.RegisterStartupScript(this.GetType(), "whatever", "closeMe();");
Learn more about AJAX. And technically, a C# code deal with pages (it may however handle HTTP requests & replies).
you can use ( if you open a new window) the opener object.
and you can also make use of the PostBackUrl
also you can make use of form1.Action
I have a button which right now is set as a type "submit". This calls the controller, execute some code and returns back to the view. When I use jquery to hide the button, I see that when I click on the button, what I have hides the button but as soon as the view is returned, the button is not hidden no more. Whereas with type "button", when I click the button, this hides the button but doesnt execute the code in the controller. Is there a way to hide the type "submit" button so when the view returns, the button is still hidden?
$('#btnAdd').click(function() {
$('#btnAdd').hide();
});
<input type='submit'> creates a button that submits a form to a server and triggers your server code. If you want the button hidden when the page comes back, you need to add logic to your page to do that. How you do this will depend on your server technology (php, .net, etc.).
The reason the behavior with <button> is different is that <button>s don't submit the form (unless you add more code to make them do that)...so the above mentioned stuff never happens. It's not so much that a <button> stays hidden as much as the page never changes/reloads. If you added code to the <button> to make it refresh the page, it'd reappear, too.
The button is shown because the page is newly displayed after submiting the form. Your "old" page, where clicked and hid the button is history.
What do you want?
Pressing a button, do something on serverside, do not change your current page:
Use a button of type button, use ajax to call the server side.
Or use a button of type submit and do what Pablo said http://en.wikipedia.org/wiki/Post/Redirect/Get on serverside.
Pressing a button, do something on serverside, give user feedback:
Use <form method="post" to markup your form. Use a submit button to call the server side. On serverside hide the submit button, if it is called by method post (calling a page with link or typing it into the address field is calling it with method = get).
What is the difference between type submit and type button?
A submit button works without javascript to send some input to serverside. The surrounding form is send to the server and the response is rendered in browser.
A button button needs a javascript onclick handler, a javascript function. The onclick handler is called when the user pressed the button.
Since the page will be reloaded upon pressing the submit button, the button will reappear. One quick and dirty to get what you want is...
First, create a hidden field
<input type="hidden" id="hidden" value="" />
Then, when you press submit, in a click event for submit button, do something like this..
$('#submit').click(function() {
$('#hiddenField').val("1");
$('#form').submit();
return false;
});
Now in your controller, use the value of hiddenField of pass some variable to the view which can be used like this...
<?php if($hidden == "1"): ?>
<input type="submit" id="submit" value="Submit" />
<?php endif; ?>
As far as the button not submitting the form is concerned, it won't submit the form, until you submit the form yourself on the click event of button. Something like this...
$('button').click(function() {
$('#form').submit();
});
Of course, as I mentioned this is a quick and dirty way to implement the function you want, there are better ways - using AJAX, also the implementation can change depending on what server side language you use (I used php over here).
When your form is submitted and your controller process the data, if certain criteria is met, you can set a temporary session variable or a cookie in server side code. So, basically the page will check for this variable on every page load. Example in PHP:
if( empty($_SESSION['temp']['hideSubmitButton']) ) {
$submitButton = '<button type="submit">Normal Button</button>';
} else {
$submitButton = '<button type="submit" disabled="disabled">Disabled Button</button>';
// or $submitButton = '';
}
But then you have to decide when to unset() the $_SESSION['temp'] or $_COOKIE['temp'] variable.