I am trying to submit the form with selenium api submit(). Click() would not work definitely because it is form button.
Element is "submit".
element.submit() but nothing is happening.
I am trying some java script solution to submit form.
Here is the html:
<form class="ng-pristinedfgd ngrg-valid">
<label class="input-width" for="userName">Username</label>
<input type="text" class="input-width form-input " name="userName" autocomplete="off">
<label class="input-width" for="password">Password</label>
<input type="password" class="input-width form-input " name="password" autocomplete="off">
<p class="forgot-utility">Need login help? Visit the utility website for direction.</p>
<button type="**submit**" class="blue-button">Share Energy Usage</button
</form>
Any help would be much appreciated. Thanks in advance.
As per the HTML you have shared you can invoke click() method as follows:
Java click():
driver.findElement(By.xpath("//button[#class='blue-button'][contains(.,'Share Energy Usage')]")).click();
Note: As the AUT is Angular based so if you are trying to invoke click() soon after Page Load you have to induce WebDriverWait as follows:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//form[#class='ng-pristinedfgd ngrg-valid']//button[#class='blue-button'][contains(.,'Share Energy Usage')]"))).click();
You can always use JS when this happens (it's kind of frequent in some applications) -
driver.execute_script("document.getElementsByClassName('blue-button')[0].click()")
You can call via following:
driver.findElement(By.className("blue-button")).click();
and as #Debanjan mentioned introduce wait.
Thanks everyone for your valuable time. I tried many options but did not work because there was extra margin on that button so Selenium was keep on loosing focus even after intentional focus on element. As I asked developer to remove the extra CSS margin on that button, issue got resolved.
Related
currently blocked with this tricky scenario
please refer to attached screen shot for code
I'm not able to change value of header.
<div class="tb-title-container unedited">
<h1 class="tb-headline-inner">Untitled List</h1>
<input type="text" maxlength="255" class="tb-title-input chromeless"> <label class="tb-list-options">
</label> </div>
code generated by Selenium IDE
I tried javascript approach as well to set value of header field , but it's not getting changed. As header name change takes place only with keyboard enter key press.
driver.findElement(By.cssSelector("label.tb-list-options > svg")).click();
driver.findElement(By.cssSelector("li.rename.blue")).click();
driver.findElement(By.cssSelector("input.tb-title-input.chromeless")).clear();
driver.findElement(By.cssSelector("input.tb-title-input.chromeless")).sendKeys("vikram");
java code
WebElement inputField = getDriver().findElement( By.xpath("//div[#class='tb-title-container']/h1[#class='tb-headline-inner']") );
( (JavascriptExecutor)getDriver() ).executeScript("arguments[0].value='VIKRAM'", inputField);
Have you tried
((JavascriptExecutor)driver).executeScript("document.getElementsByClassName('tb-title-container').innerHTML='vikram'");
So I have basically been stuck on a particular piece of code for a few days now. I am trying to go to a webpage and click a specific toggle button using Htmlunit in Java.
The code I currently have for the java program:
WebClient webClient = new WebClient();
HtmlPage page1 = webClient.getPage("webpage URL");
page1.getElementById("additional_parameters_toggle").click();
The HTML code for the webpage toggle:
<div class="parameters clearfix">
<input type="checkbox" id="additional_parameters">
<label for="additional_parameters class="additional_parameters_toggle" data-name="big old ugly toggle">
<span class = "checkbox_outer">
<span class = "checkbox_inner"></span>
</span>
<span class="label_text">Show details / hourly data</span>
</label>
</div>
I figured that should work but I keep getting a NullPointerException. If there is anyone that could offer some insight or help in anyway, it would be greatly appreciated. Thank you.
As Tom has hinted, .getElementById() will not work since there is no id HTML attribute of that value.
You need to use:
page1.<HtmlElement>getFirstByXPath("//label[#class='additional_parameters_toggle']").click();
which means, find the first element by XPath: search for any label with a class attributes of "additional_parameters_toggle", then .click() it.
page1.getElementById("additional_parameters_toggle")
Looks like you don't have an element having this value as an Id. You do however have an element with an attribute class containing this value.
I'm facing an issue as to redirecting my website to a certain specific URL...
to better explain, I'm adding an auto refresh meta to the top of my jsp that refreshes the web page every 15 minutes, the only problem here is that when the 15 minutes are over, instead of refreshing the same page (thus calling the same servlet to recalculate what needs to be recalculated) it redirects to the home page!(defined in xml as login.jsp)
Now I've tried to debug the code to see which part is redirecting to the home page but it seems that it's no power of my own! it's probably something Tomcat is doing that I'm not aware of. It already happened to me before and the solution was to add an attribute to the session scope session.setAttribute("User",user);
but not this time...
To support my point(It's not a session problem), here's a portion of the code directly extracted from the page source code after redirection
<header>
<h1>WelcomeTV</h1>
</header>
<section>
<form action="Login" method="post">
<ul>
<li><label for="username">Username</label></li>
<li><input type="text" name="user" id="username" placeholder="Your Username" value="wtv_administratifs"></li>
<li><label for="password">Password</label></li>
<li><input type="password" name="password" id="password" placeholder="Your Password"></li>
<li><input type="submit" value="Log in"></li>
</ul>
<p class="reset_pwd">Reset your password</p>
<p class="change_pwd">Change your password</p>
</form>
</section>
You can see that the value is filled, and in my jsp the value is taking ${sessionScope.username} so this proves that the session is still valid
Please help me? if it's not clear please let me know, I'll try to make myself clearer.
Set up your session config in your web xml to -1 if you do not want it to expire
Well, This is embarrassing... It seems the little problem I have has nothing to do with the previous one I resolved with an input into the session scope.
The issue was simply that the URL after logging into my website stays //Context/Login (HTTP POST ><) and with the meta auto refresh, it does not refresh the servlet called after the login, it actually refreshes the same URL displayed above => Login and thus redirects me to the login page...
Issue resolved by adding
<META HTTP-EQUIV="refresh" CONTENT="900;URL=http://10.84.18.53:8080/Welcome_TV/display"> which is the servlet that needs to do all the recalculation.
Issue resolved, thank you for you comments and terribly sorry for your time :)
I am trying to update the existing code of other developer. I am facing the problem of confusing actions.
Existing :
<s:form name="f2" action="delFood.action">
<input type="submit" value="Delete" class="button" onClick="javascript:get_check_value()"/>
My Code to Update:
<input type="file" class="button" id="foodItemFile" name="foodItemFile" value="Browse ..."/>
<input type="submit" class="button" value="AddFood" onClick="callAddFood();"/>
My Javascript:
In my script, I try to submit my action by following code.
document.f2.action = "AddFoodAction.action";
document.f2.submit();
It seems like when I click [AddFood] button, it always call the [delFood.action].
For adding food, I need to check something with javascripts before calling the [AddFoodAction.action] action.
Due to limitations, I can't change the existing code. I can only add new codes to the existing one.
So, Any way to call [AddFoodAction.action] from javascripts without confusing with other actions of the same form ?
Thanks ahead.
Looks like a javascript problem here. Make sure the code
document.f2.action = "AddFoodAction.action";
document.f2.submit();
is executing. Maybe document.f2 is not resolving correctly (maybe more than one form with this name?).
This fiddle shows that it should work. It changes the action of a form inside a onclick handler on a <input type="submit">.
And just a reccomendation, don't do document.f2.submit();. It's an <input type="submit"> so it will submit the form automatically when onclick ends.
Finally, I resolve it by doing like that.
1) There will no direct action in form tag.
<s:form name="f2" method="post" enctype="multipart/form-data">
2) for delete part,
<input type="submit" value="Delete" class="button" onClick="javascript:get_check_value()"/>
call the delete action from the javascript, not directly from form.
document.f2.action = "delFood.action";
3) for add part, like delete part. check necessary things in java scripts and call the add action. It works well.
Another Solution:
There maybe common action directly called from Form. For this approach, just name your button and give value and map those value from action class. And differentiate multiple methods by using those value from one action. I read this article at coderanch and javaSample. Thanks.
I am trying to click on the image input type using webdriver, but it's not working.
The html code:
<input type="image" onclick="validationAndSubmit('next'); return false;" alt="Continue" src="/subscriptions/versions2/commonlab/images/buttons/continue.gif" name="continue">
The code I am using to click is:
driver.findElement(By.name("continue")).click();
I could invoke the Javascript like below, but that is not my intent.
((JavascriptExecutor)driver).executeScript("validationAndSubmit('next'); return false;");
I would like to simulate end user experience on IE as much as possible.
What should I do in order to click the image input?
thanks
Jenga