I am not able to click on the submit button inside the modal. It only works sometimes - it's not stable.
Below is my HTML element:
<button id="submit-btn" name="submit" data-dismiss="modal" type="submit" class="btn btn-info btn-sm submit projectSaveBtn">Submit</button>
Here I'm using id for locating the element, but I'm not able to click on the submit button inside the modal.
My Java code:
WebElement element1 = driver.findElement(By.id("submit-btn"));
Actions actions = new Actions(driver);
actions.moveToElement(element1).build().perform();
wait.until(ExpectedConditions.elementToBeClickable(element1)).click();
When we use the action class, selenium takes mouse and keyboard controls. If we interact with mouse or keyboard at the time of test execution (specially the actions statement execution), it may fail some times.
You can try with actions class and don't interact until it completes the test execution. it may solve your issue.
Instead of using the id attribute you can use an xpath as follows :
WebElement element1 = driver.findElement(By.xpath("//button[#id='submit-btn']"));
element1.click();
or
WebElement element2 = driver.findElement(By.xpath("//button[#class='btn btn-info btn-sm submit projectSaveBtn' and #id='submit-btn']"));
element2.click();
If it's a windows modal dialog box, you might want to first switchTo() to the modal and then perform accept() on it.
driver.switchTo().alert().accept();
driver.switchTo().window("");
I solved the issue, it was timing issue .Modal was opened before the page loaded properly so giving the time before clicking/opening modal solves the issue .
Here is my code -->
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement add = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.btn.projectAddBtn")));
add.click();
This solved my issue.Thank you all for the support :-)
Related
I'm trying to click a checkbox but it's keep clicking the link 'terms and conditions'. Although my xpath (mentioned below) work on a minimized window but it's failing to click the checkbox when the window is maximized because the href (image) appears in the second line next to checkbox. Looking for some suggestions on clicking the checkbox widget on maximized window. I need to get a focus on it.
Interestingly, when i hover over the ::before (css selector) only than the widget gets highlighted.
<div class="checkbox u-mar-bot-5">
<div class="checkbox__container">
<input class="checkbox__input" type="checkbox" id="basket-contact-terms" required data-parsley-multiple="basket-contact-terms" style>
<label class="checkbox__label checkbox__label--has-link checkbox__label--small" for="basket-contact-terms" style>
::before
"I have read and agree to " <a class="text-link text-link--base text-link- small" href="/terms-conditions" target="_blank">Terms and Conditions</a>
</label>
</div>
</div>
image: Terms and Conditions
I tried a few options that keep failing to check the box and instead the link 'terms and conditions' gets the click. I must be missing something basic.
driver.findElement(By.xpath("//label[#for='basket-contact-terms']")).click();
driver.findElement(By.xpath("//label[contains(#class,'checkbox__label checkbox__label--has-link checkbox__label--small')]")).click();
I did looked around and found someone suggested to use this (below) so i tried but didn't work:
WebElement elem = driver.findElement(By.xpath("//div[contains(#class,'checkbox u-mar-bot-5')]"));
Actions action = new Actions(driver);
action.moveToElement(elem).click().build().perform();
Any suggestion would be appreciated.
Since you tried the ID of the INPUT and it threw an error that it wasn't visible, I would first try a wait to see if it will become visible. (I'm assuming it won't but it's worth a try first).
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.id("basket-contact-terms"))).click();
If that doesn't work, I would next try to click a different position on the element. By default, Selenium clicks on the center of the element. In your case, I think this is what's causing the issue. You can use Actions to click the upper left (1,1) of the element.
WebElement label = driver.findElement(By.xpath("//label[#for='basket-contact-terms']"));
new Actions(driver).moveToElement(label, 1, 1).click().perform();
You can try with
WebElement elem = driver.findElement(By.id("basket-contact-terms"))
I have submit button, which is only one on the page, and it's in form.
html part:
<form class="search-form ng-touched ng-dirty ng-valid" novalidate="" style="" xpath="1">
<div class="row">...</div>
<div class="row">...</div>
<div class="row">...</div>
<div class="form__actions" xpath="1">
<div class="form__buttons">
<!---->
<div class="btn__wrapper">
<button class="btn btn__primary" type="submit">
Select My Car
</button>
</div>
</div>
</div>
</form>
So, I'm taking xpath:
//button[#type='submit']
I'm successfully pressing it via submit() (let me skip WebDriver init, its fine):
WebElement searchButton = driver.findElement(By.xpath("//button[#type='submit']"));
searchButton.submit();
(and some search performs)
But when I'm trying to press it via click()
WebElement searchButton = driver.findElement(By.xpath("//button[#type='submit']"));
searchButton.click();
it's not pressed in browser which is launched, and same time Junit test is green (not test, but just pressing button):
#Test
public void test() {
WebElement button = driver.findElement(By.xpath("//button[#type='submit']"));
button.click();
}
Can please someone explain, why submit() successfully presses button in such case, but click() - no. And I do not understand, why "test" is green, when we are trying to click(), but it was not performed, if looking on browser launched by driver.
UPDATED:
I tried
WebElement button = driver.findElement(By.xpath("//button[#type='submit']"));
if (button.isEnabled()) {
button.click();
}
and
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(button)).click();
but still the same - submit() works fine, click() - does not.
The method object.submit() is there for submitting the form to the server. It has another advantage, in case if you're not able to locate the "submit" button then you can take any object of the form and trigger submit() function.
It seems in searchButton.submit(); searchButton is an element of the form and the submit action on it triggers submission of form on server.
Now, why searchButton.click(); not working here could have following reasons.
The button is visible but not enabled.
Driver is finding the 2
instances of searchButton element.
Suggestion: Evaluate following code and check it returns more than one element. If it does then you're clicking on the wrong instance.
List<WebElements> e = driver.findElements(By.xpath("//button[#type='submit']"));
Also try,
driver.findElement(By.xpath(".//button[#class='btn btn__primary'][#type='submit']")).click()
http://docs.seleniumhq.org/docs/03_webdriver.jsp#user-input-filling-in-forms
submit()
submit() method is defined as :
void submit()
Throws:
NoSuchElementException - If the given element is not within a form
As per the JavaDocs when you invoke submit() on an WebElement, if this current element is a form, or an element within a form, then this will be submitted to the remote server. If this causes the current page to change, then this method will block until the new page is loaded.
click()
click() method is defined as :
void click()
Throws:
StaleElementReferenceException - If the element no longer exists as initially define
As per the JavaDocs when you invoke click() on an WebElement and if this causes a new page to load, you should discard all references to this element and any further operations performed on this element will throw a StaleElementReferenceException. Note that if click() is done by sending a native event (which is the default on most browsers/platforms) then the method will not wait for the next page to load and the caller should verify that themselves. There are some ExpectedConditions for an element to be clicked. The element must be visible and it must have a height and width greater then 0.
Your Usecase :
In your usecase, the target WebElement //button[#type='submit'] is within a <form> tag. You are able to successfully press it via submit() as it blocks the method untill the new page is loaded completely as a result of submission on the previous page. Hence the following works :
WebElement searchButton = driver.findElement(By.xpath("//button[#type='submit']"));
searchButton.submit();
But when you try click() method as below, click() being a native event doesn't waits for the new page to load completely. Hence invoking click() method fails.
WebElement button = driver.findElement(By.xpath("//button[#type='submit']"));
button.click();
Your Code Trials :
Validating the button.isEnabled() condition won't fetch us the required result if the element is clickable or not as isEnabled() validates Is the element currently enabled or not? This will generally return true for everything leaving out the disabled input elements. But necessarily isEnabled() wouldn't be validating if the WebElement is visible, interactable and clickable or not.
Solution :
As per the details provided, the solution to invoke click() method would be to induce WebDriverWait with ExpectedConditions clause set to elementToBeClickable as below :
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#type='submit']"))).click();
Update :
Best Practice :
As per the updated HTML and your subsequent comment where you mentioned about Angular4 being used in your app, a much more effective way would be to construct the xpath mocking the HTML DOM as follows :
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//form[#class='search-form ng-touched ng-dirty ng-valid']//button[#class='btn btn__primary' and #type='submit']"))).click();
I have a iframe, then a widget that contains the create invoice button. It is not inside the iframe but outside of it and I cannot access it.I cannot access this dialog using selenium webdriver java, Im trying to access the create invoice button using java:
</div><div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix"><div class="ui-dialog-buttonset"><button type="button" id="saveBtnDialog" class="btn btn--primary">Create Invoice</button><button type="button" id="CancelBtnDialog" class="btn btn--primary">Cancel</button></div></div></div>
my code:
private final By createInvoiceBtn = By.id("saveBtnDialog");
driver.switchTo().frame(driver.findElement(By.cssSelector(".ui-dialog-buttonset")));
driver.findelement(createInvoiceBtn).click();
As my understanding if your button is not inside a frame and you are already in the frame
You need to switch from frame to defaultContent and then go to find element and perform action as below :-
//first switch to out side the frame
driver.switchTo().defaultContent();
//now implement WebDriverWait to provide wait
WebDriverWait wait = new WebDriverWait(driver, 100);
//wait until ExpectedConditions is not true
WebElement buttonEl = wait.until(ExpectedConditions.elementToBeClickable(By.id("saveBtnDialog")));
//now you go for the action
buttonEl.click();
Hope it will help you...:)
I am trying to automate functional testing of a web application using Selenium webdriver and Java.
In the AUT, there is a 'Submit' button defined by the following html code
<button id="submitbtn" class="btn btn-primary" type="submit">Submit</button>
I use the following command to click the button.
driver.findElement(By.id("submitbtn")).click();
When I run the code, the webdriver can find the button but the click action is not performed (I can understand that webdriver can find the button because no exception is thrown and the I can see a selection on the button when the code is run). I tried different waits
new WebDriverWait(driver,60).until(ExpectedConditions.elementToBeClickable(driver.findElement(By.id("submitbtn"));
but not getting any positive result. If I use,
Thread.sleep(3000);
it works fine (but I want to avoid this code). I tried all other types of waits and action class,
Actions action=new Actions(driver);
action.moveToElement(driver.findElement(By.id("submitbtn"));
wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.id("submitbtn")));
action.click().perform();
but no luck. Is there any way to achieve this?
How about JavascriptExecutor?
WebElement element = driver.findElement(By.id("submitbtn"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
A submit() is an option driver.findElement(By.id("submitbtn")).submit();. More information here
I am trying to code in java using selenium webdriver to click on Dropdown list in walmart page. But I am unable to access the li elements.
<button class="js-flyout-toggle dropdown" aria-haspopup="true" type="button" data-cat-id="0" aria-expanded="false"> All </button>
<div class="js-flyout-modal flyout-modal">
<ul class="block-list">
<li><button class="no-margin font-semibold" type="button" data-cat-id="0" tabindex="-1"> All Departments </button></li>
<li><button class="no-margin font-semibold" type="button" data-cat-id="91083" tabindex="-1"> Auto & Tires </button></li>
<li><button class="no-margin font-semibold" type="button" data-cat-id="5427" tabindex="-1"> Baby </button></li>
I wanted to access Baby using selenium webdriver in Java.
Below is my code:
driver.get("http://www.walmart.com");
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement dropdown = driver.findElement(By.xpath("html/body/div[2]/header/div[3]/div/div/div/div/div[3]/form/div/div[1]/div/button"));
dropdown.click();
List<WebElement> liElements = driver.findElements(By.xpath("//*[#class='block-list']/li"));
for ( WebElement we: liElements) {
System.out.println(we.getText());
}
But this gives an error as below:-
"Exception in thread "main"
org.openqa.selenium.ElementNotVisibleException: Element is not
currently visible and so may not be interacted with Command duration
or timeout: 132 milliseconds".
Please help
You defined WebDriverWait but you don't use it
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement dropdown = wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("dropdown")));
Implicit wait will wait for element to exist in the DOM. If you want to use explicit wait you need to use Expected Conditions as well. Only defining WebDriverWait doesn't actually do anything.
Your code looks fine. Please try below xpath :-
//div[#class='js-flyout-modal flyout-modal']//ul[#class='block-list']/li
Now first try to put thread.sleep if it works then it is problem of wait only
Thread.sleep(30000);
But then don't use Thread for your script as it's not recommended .. It's just to ensure that script is failing because of time
Hope it will help you :)
May be other element in walmart page match your xpath //*[#class='block-list']/li and that element is not visible.