Selenium Webdriver - unable to click on button - java

HTML code:
<div class="buttonBG">
<input type="button" onclick="window.location.href='?mod=eA&act=00001';" class="buttonGreen" value="TK">
<input type="button" onclick="ttoggle('carianDiv');" class="buttonGreen" value="CK">
</div>
Below is my java code, when I try with below code. Can I know whats wrong is in my selenium webdriver code:
driver.findElement(By.xpath("//input[#class='buttonGreen' and contains(#onclick, 'window.location.href='?mod=eA&act=00001')]")).click();

Try to search by value
driver.findElement(By.cssSelector("[value='TK']")).click();
And for what's wrong, you are searching for ?mod=eA&act=00001 when in the html its
?mod=eA&act=00001
Edit
Another solution is to insert the buttons to list and click by index:
List<WebElement> buttons = driver.findElements(By.className("buttonGreen"));
buttons.get(0).click();
You can also try using explicit wait
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[value='TK']")).click();
This will wait up to 10 seconds for the button to be visible before clicking on it. You can change the selector or time span.

Try using XPath instead of CSS
driver.find_element(By.XPATH, '//input[#onclick=\'window.location.href=\'?mod=eA&act=00001\';\']').click()
Edit
Here is the code to switch to iFrame,
driver.switchTo().frame("frame_name");
NOTE: After completing the operation inside the iframe, you have to again return back to the main window using the following command.
driver.switchTo().defaultContent();

Related

Problem with button with IE driver and Xpath Selenium with Java

i have the follow problem:
I have a button inside a form (the web page have a login and password text box and a button), this button calls a js funtion and after the login and password validation, calls the main web page. The html code is this (this code is inside of a form calls "login" and method = POST):
<INPUT class="btn btn-mini btn-primary" onclick=submitForm(); type=button value="Sign On">
In Selenium i try with the follow statements, but without success:
driver.findElement(By.xpath("//input[#type='button']")).click();
driver.findElement(By.cssSelector("input[type='button'][#value='Sign On']")).click();
driver.findElement(By.xpath("//input[#value='Sign On']")).click();
when i run the script, login and password text are filled correctly, but the click in the button it's no working.
Could you help me with this?
Thanks!
Gonzalo from Chile
To click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.btn.btn-mini.btn-primary[value='Sign On'][onclick^='submitForm']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='btn btn-mini btn-primary' and #value='Sign On'][starts-with(#onclick, 'submitForm')]"))).click();
i found the solution, reading a lots of issues related with this. I'm working with win 10 and IE 11, so, Selenium has a problem with this, because need the size of the text, apps, and others items in 100%. In my case, i had this configuration in 150%. Fixing this, i run the script one again and it works.
The code used is:
WebElement button = null;
List<WebElement> inputs = webDriver.findElements(By.tagName("input"));
for (WebElement input : inputs) {
if (input.getAttribute("value").equals("Log In")) {
button = input;
break;
}
}
if (button == null) {
System.err.println("Cannot find button!");
} else {
System.out.println("Clicking button now!");
button.click();
}
After this, i check another code more efficient:
driver.findElement(By.cssSelector("input[type='button'][value='Sign On']")).click();
This works too.
thanks all off you for your help

Error as Element is not clickable at point in selenium

Dom structure :
<li class="slds-dropdown-trigger slds-dropdown-trigger--click slds-m-left--
x-small" data-aura-rendered-by="534:20;a">
<!--render facet: 537:20;a-->
<!--
render facet: 541:20;a--><button class="bare slds-button uiButton
forceHeaderButton oneUserProfileCardTrigger" aria-live="off" type="button"
data-aura-rendered-by="184:190;a" data-aura-class="uiButton
forceHeaderButton
oneUserProfileCardTrigger"><!--render facet: 185:190;a--><!--render facet:
187:190;a-->
<div class="tooltipTrigger tooltip-trigger uiTooltip" aria-describedby="tt-
for-174:190;a" tabindex="-1" data-aura-rendered-by="179:190;a" data-aura-
class="uiTooltip">
<span data-aura-rendered-by="171:190;a" class="uiImage"
data-aura-class="uiImage"><img data-aura-rendered-by="169:190;a"
src="https://c.ap5.content.force.com/profilephoto/005/T/1"
class="profileTrigger" alt="">
</span><span class="tooltip-invisible"
role="tooltip" id="tt-for-174:190;a" data-aura-rendered-by="181:190;a">View
profile</span>
</div></button><!--render facet: 543:20;a-->
</li>
i tried these lines of code for Logout :
First click on Logout symbol:
WebDriverWait wait3 = new WebDriverWait(driver, 20);
driver.findElement(By.xpath("//img[#class = 'profileTrigger']")).click();
JavascriptExecutor jse = (JavascriptExecutor)driver;
/*exe1.executeScript("arguments[0].click();", newbt);*/
jse.executeScript("scroll(250, 0)");
second click on Logout button :
driver.findElement(By.xpath("//a[#class =
'profile-link-label logout uiOutputURL']"));
I am getting error as Element is not clickable at point.
#SrieedherSanthakumar
I have few that would like to clarify first, whether or not are you dependent on wait call. Like you are clicking on something and want to wait for something and then process with your second click ?
If so, you might have to modify your first call something like below :
WebDriverWait wait3 = new WebDriverWait(driver, 20);
driver.findElement(By.xpath("//img[#class = 'profileTrigger']")).click();
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
JavascriptExecutor jse = (JavascriptExecutor)driver;
/*exe1.executeScript("arguments[0].click();", newbt);*/
jse.executeScript("scroll(250, 0)");
once you have waited for your page to be load or expected id is visible then proceed with your second call. I am not able to see whether you are waiting for your object in the first call to load since you are clicking on profileTrigger before moving forward with your second call.
1.Use scrollTo element by selector - this will ensure element is visible:
WebElement element = driver.findElement(By.xpath("//button[#class='oneUserProfileCardTrigger']"));
((JavascriptExecutor) driver)
.executeScript("arguments[0].scrollIntoView(true);", element);
2.Always use delay or wait after scroll before click. It takes time for browser.
3.If nothing helps - use javascript click. Bad solution as it not like real user do, but will work always:
WebElement element = driver.findElement(By.xpath("//button[#class='oneUserProfileCardTrigger']"));
((JavascriptExecutor)driver)
.executeScript("arguments[0].click();", element);

how to find xpath in selenium webdriver

Following is the HTML of the page, How do i get the Xpath or is there anyother way to automate using java?In fact, we shud click on this "continue" button.
<regform-button>
<button ng-disabled="activityIndicator" ng-click="validate()" type="button">
<div template="api-loader" ng-http-loader="">
<div class="http-loader__wrapper ng-hide" ng-show="showLoader" ng-include="template">
<span class="api-loader"></span>
</div>
</div>
<ng-transclude>
</button>
</regform-button>
If you are using Google Chrome, right click on the button, and select Inspect in the popup. The html will open in a developer tools frame. Right click on the element in the developer tools frame, hover over copy, select copy xpath.
Here is the XPath to the form on the URL.
//*[#id="main-frame"]/div[1]/div[2]/div/div[1]/div[1]/form/regform-steps/ng-transclude/regform-step[1]/ng-form/ng-transclude/regform-button/button
WebDriver driver = new ChromeDriver();
driver.get("https://uk.match.com/unlogged/landing/2016/06/02/hpv-belowthefold-3steps-geo-psc-bowling?klid=6740");
//fill in fields
WebElement element = driver.findElement(By.xpath("//*[#id=\"main-frame\"]/div[1]/div[2]/div/div[1]/div[1]/form/regform-steps/ng-transclude/regform-step[1]/ng-form/ng-transclude/regform-button/button"));
element.click();
driver.findElement(By.xpath("//regform-button/button")).click();
Try opening the console for Google Chrome and typing the following:
document.evaluate("//regform-button/button", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.click()
It works...that's if you've specified the required fields. Also be sure to wait for a while (2 secs to be sure) after specifying the required fields as there might be an additional load after setting the required fields.
Edit: The code is javascript..I just took the xpath given by the previous answers and used that to locate the element through js. I'm saying that "//regform-button/button" should work..
Once you have selected the required values from the drop-downs appearing before Continue button, you can click on Continue button using the following statement:
driver.findElement(By.xpath("//span[text()='Continue']")).click();
The following xpath worked for me.But I dont know, why is it different for each different browser.
For Firefox :
`driver.findElement(By.xpath("//*[#id='main-frame']/div[1]/div[2]/div/div[1]/div[1]/form/regform-steps/ng-transclude/regform-step[1]/ng-form/ng-transclude/regform-button/button")).click()`;
For chrome: driver.findElement(By.xpath("//regform-button/button")).click();

Selecting a value from <md-select> tag using Selenium Webdriver

I am using Selenium Webdriver and working on automating an AngularJS Web App on Chrome. It was going pretty well until I hit a dropdown list on the app.
My test keeps crashing everytime I try to select a value from it. I have been doing my research on this and I have only seen 2 solutions (both of which I have tried but don't work)
Use the Select object. This doesn't work because the html tag is not <select>, its <md-select> and this fails the test.
I then tried to just click on the dropdown element and click on the value - driver.findElement(By.xpath("xpath to dropdown list")).click(); and driver.findElement(By.xpath("xpath do dropdown value")).click();
With example 2, I also tried creating it as a WebElement variable and calling click() separate, but this did not work either.
Any ideas on how I can solve this issue?
Update
HTML for the dropdown list
<div ng-switch-when="dropdown" class="ng-scope">
<zf-form-dropdown>
<div class="dropdown">
<div layout="row">
<div flex="50" class="quote-label">
<p ng-bind-html="::label" class="ng-binding">Title</p>
</div>
<div ng-show="false" flex="10" class="tooltip-icon ng-hide" ng-click="showToolTip(field.get('toolTip'))" role="button" tabindex="0" aria-hidden="true"><img src="img/item-question#2x.png"></div>
<md-select flex="" ng-disabled="quote.isRated() || !input.enabled" ng-change="onDropdownChange()" ng-model="input.value" class="ng-valid md-default-theme ng-touched ng-dirty" role="combobox" id="select_0Q9" aria-haspopup="true" aria-expanded="false" aria-labelledby="select_label_0I1" tabindex="0" aria-disabled="false" aria-invalid="false" aria-owns="select_menu_0Q8"><md-select-label class="md-select-label md-placeholder" id="select_label_0I1"><span></span><span class="md-select-icon" aria-hidden="true"></span></md-select-label></md-select>
</div>
</div>
</zf-form-dropdown>
</div>
HTML for the value I want to select
<md-option ng-value="::item.val" ng-selected="item.checked" ng-repeat="item in getOpts()" tabindex="0" class="ng-scope" role="option" aria-selected="false" id="select_option_0QD" value="MR">
<div class="md-text ng-binding">Mr</div>
<div class="md-ripple-container"></div>
</md-option>
The xpath for the dropdown list is //*[#id="select_0Q9"]
The xpath for the dropdown value is //*[#id="select_option_0QD"]
If you are sure that your Xpath is fine then you can also click using javascriptexecutor so try it out like below:-
WebElement element= driver.findElement(By.xpath("//md-option[#id='select_option_0QD']/div[1]"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
Please feel free to locate the element in above code as per your convenience .
As per me your xpath of dropdown should be like below:-
//md-option[#id='select_option_0QD']
And xpath of first div which I suppose want to click is:-
//md-option[#id='select_option_0QD']/div[1]
Change [1] to [2] if you want 2nd value.
In an another aspect you can also store all your element in the list(As you know you can't use select) and click them as per your need or all.
For that you need to use xpath like:-
//md-option[#id='select_option_0QD']/div
Now implement it into code:-
List<WebElement> allelemts = driver.findElements(By.xpath("//md-option[#id='select_option_0QD']/div"));
for(WebElement ele: allelemts){
driver.findElement(By.xpath("//md-option[#id='select_option_0QD']")).click();
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", ele);
}
Hope it will help you :)
Since you are receiving a NoSuchElementException exception, I believe the issue lies in Selenium not able to identify the element. Try any of the following wait methods and then try to click the element.
Explicit Wait: (Most Preferred)
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("elementID")));
or
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("elementID")));
Implicit Wait:
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Sleep: (Try to avoid this unless absolutely necessary)
Thread.sleep(1000);
EDIT: Added code to check for element's presence using findElements method.
Before using any of these wait methods you could also check for the presence of your element using findElements method.
List<WebElement> element = driver.findElements(By.id("elementId"));
if (element.size() == 0) {
System.out.println("Element not found");
} else{
System.out.println("Element found");
}
I have finally solved the issue!
I got the Selenium IDE and recorded a session where I got as far as selecting the dropdown menu and selecting my value. I then exported the file as a java test case and was able to read the lines where it selected the values, and they were;
driver.findElement(By.cssSelector("#select_08D > #select_label_005 > span.md-select-icon")).click();
driver.findElement(By.id("select_option_08H")).click();
So first off they both do not use xpath to find the elements, the dropdown menu itself is found with the cssSelector and the value is found by the id.
I am just cross referencing again and the id for the value is select_option_08H but while I am looking at Google Console I can see the id for the value is select_option_189

Unable to upload file using sendKeys on input element

In order to avoid window dialog of upload file, I'm trying following:
driver.findElement(By.xpath("//span[#class='ab-attachment-item']/input")).sendKeys(filePath);
Following is the HTML code snippet:
<div class="ab-attachments">
<span class="ab-attachment-item" ng-hide="isReadOnly()" style="background-color: transparent;">
<input class="ab-attachment-input ng-isolate-scope firefinder-match" type="file" rx-file-upload="file" accept=".pdf,image/*" style="background-color: transparent;">
But it results into error:
org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
I want to confirm if this error is because of ng-hide="isReadOnly()" in span tag?
And how this can be resolved using Selenium WebDriver itself(using JavaScriptExecutor or something)?
For handling this dialog other tools like Sikuli, AutoIt can be used; but I want to avoid that overhead.
You should be able to do this:
driver.findElement(By.className("ab-attachment-input ng-isolate-scope firefinder-match")).sendKeys(filePath);
If not try adding an implicit wait before you try and sendKeys
Int timeoutInSeconds = 10;
WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("ab-attachment-input ng-isolate-scope firefinder-match"));
//I would use implicit wait and directly call the input element instead
new WebDriverWait(driver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[rx-file-upload='file'][accept=".pdf,image/*"]"))).sendKeys("filePathWithExtension");

Categories

Resources