How can I select this button? - java

I am using Selenium to automate the checkout process of a website and can not figure out how to select this certain element properly. So far I have tried all of the following with no luck:
//no such element
driver.findElement(By.id("Something here")).click();
//no such element
driver.findElement(By.xpath("//*[#id="add-remove-buttons"]/input")).click();
//no such element
driver.findElement(By.name("commit")).click();
It seems like the closest I've gotten is with:
driver.findElement(By.className("button")).click();
Using this gives me an error saying the button is not visible. So it looks like I'm heading in the right direction with this, but I'm not sure where to go from here. Here is the HTML source of the button:
<input type="submit" name="commit" value="add to cart" class="button">
not sure if this plays a part in it but when this button is clicked it changes into a new button. The same location, but different color, text, and functionality.
If you wanted to take a look for yourself here is the website:
http://www.supremenewyork.com/shop/accessories/yf89tm27c/e8c56njah?alt=0
and the button I am trying to click is the add to cart button.

Try the following code:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.name("commit")));
button.click();
Hope it helps you!

Related

How to click on an element inside span tag? - Selenium

Hi I am trying to click on a specific button with text "€11" on it in selenium webdriver in Java.
The button is located within this format and there is also another button on the page with exactly the same code but different price:
<button type="button" class="bui-button bui-spacer--medium bui-button--primary bui-button--wide">
<span class="bui-button__text">€11</span>
</button>
How would I do this. Any help is appreciated :)
Full CSS
I know some ways that could help
javascript:
document.querySelector("span.bui-button__text").click()
jquery:
$("body > button > span").click()
Selenium (Java):
driver.findelement(By.cssSelector("button[class='bui-button bui-spacer--medium bui-button--primary bui-button--wide']").click()
I hope that they help you !
A few options you could use:
driver.findElement(By.xpath("//span[contains(text(),'€11')]")).click();
Click first button using this:
driver.findElement(By.xpath("(//span[#class='bui-button__text'])[1]")).click();
Click second button using this:
driver.findElement(By.xpath("(//span[#class='bui-button__text'])[2]")).click();
Note: use wait's if you have any delays in loading amount

Checkbox in iframe - how to check that there is a checkbox and if yes click on it

I am using Java and chromedriver. So far I used the following code to simply click on a button, then a small window popped up and I clicked another button. Interestingly I had not to change to any frame. All worked fine. Here is the code:
// click on button
driver.findElement(By.xpath(hyperlink_take_order)).click();
// a second small window popped up and I clicked another button
driver.findElement(By.xpath("//a[contains(text(),'accept')]")).click();
So far I used xpath to identify the buttons I need to click.
From time to time there is an additional checkbox (sometimes multiple checkboxes) on the second small window which I all need to click on. I tried to find them via xpath but found out that xpath did not work here. All I get in HTML is something like this:
The number in the brackets [] in this case 17232 vary each time, therefore I cannot find a name I can use for the checkbox. I read many articles on stackoverflow and found this peace of code which also did not work.
// click on button
driver.findElement(By.xpath(hyperlink_take_order)).click();
// a second small window popped up
try
{
driver.switchTo().frame(driver.findElement(By.tagName("iframe")));
System.out.println("change to iframe worked");
List<WebElement> CHECKBOXlist = driver.findElements(By.xpath("//input[#type='checkbox']"));
for(WebElement checkbox : CHECKBOXlist)
{
System.out.println("there was a checkbox");
System.out.println(checkbox.getAttribute("name"));
checkbox.click();
}
driver.switchTo().defaultContent();
}
catch (Exception e)
{
System.out.println("there was no checkbox");
}
driver.findElement(By.xpath("//a[contains(text(),'accept')]")).click();
<div class="md-checkbox product-service md-theme-whitebackground">
<div tabindex="0" class=md-checkbox-container">
<input name="service[17232]" tabindex="-1" id="service_17232" type="checkbox" value ="1" />
</div>
<label class="md-check-label" for="service_17232">This one is blue</label>
</div>
Do you have any idea how I can simply click on all (sometimes multiple) checkboxes on the second small window? Thanks

How to switch to Md-dialog in selenium?

I am having an issue in regards to a md-dialog pop-up in an angularJS app that I am unable to click in selenium. When i click a button the dialog box appears and becomes the active element on the screen, darkening the background. I have tried switchTo with active element, frame, alert and none of these seem to work. My most recent attempt was trying to swap windows using the below code:
winHandleBefore = driver.getWindowHandle();
Set<String> numOfWindows = driver.getWindowHandles();
System.out.print(numOfWindows.size());
for(String winhandle : driver.getWindowHandles())
{
driver.switchTo().window(winhandle);
report.updateTestLog("Switched to window", "", Status.PASS);
}
the S.o.p for the size is always outputted as 1. There is a wait in after the initial button is clicked before the popup appears to assure it has the proper time to appear. Not sure what else there is to do, I have been scouring the internet for an answer and I haven't come across anything that will let me click the elements in that popup
Edit: This is the html for the md-dialog
<md-dialog class="quote _md md-transition-in" aria-label="Summary" role="dialog" tabindex="-1" id="dialogContent_78" aria-describedby="dialogContent_78" style="">
Edit 2: Forgot to say, I am trying to click a button inside the md-dialog popup
Edit 3: After reviewing the code a bit more I noticed that the md-dialog popup has a container div that is taking up the entire screen and that it is also calling in html from another file. The container div html:<div class="md-dialog-container ng-scope" tabindex="-1" style="top: 972px; height: 769px;">
In case someone has same issue the pop-up was registering as hidden even though it had appeared on screen, so any attempt to click a button was futile as the button was seen as hidden. The workaround was to find the button using the findElement() method, assign it to a variable and then use a javascriptExectutor to click it even though it was hidden. This is not ideal if you are trying to reproduce user input but it is a workaround. Code is below.
WebElement hiddenButton = driver.findElement(uniqueIdentifier);
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", hiddenButton);
report.updateTestLog(flowName, uniqueIdentifier + " Hidden Button Pressed", Status.DONE);
Hope this helps anyone who finds it.

span class locator is not visible/clickable - selenium

Following is the HTML for span:
<td class="header-logout-btn">
<a href="logout.htm" class="btn switch-btn">
<i class="fa fa-times"></i><span class="hidden-xs">Home</span>
</a>
</td>
Tried with:
driver.findElement(By.linkText("HOME"));
driver.findElement(By.xpath("//div[#class="header-logout-btn"]/span[#class="hidden-xs"]));
driver.findElement(By.xpath("//span"));
driver.findElement(By.className("hidden-xs"));
Try following solution :
Xpath= //*[contains(text(),'Home')]
Hope it will help you.
Normally the logout button will be submenu or it will be visible only if you click or mouse over on main menu usually the main menu is profile icon. I assume this logout button visible only after clicking on main/profile button. This may be the reason the button is hidden or not clickable. The solutions are given below.
1. First click or mouse over on main/profile menu then click on logout button.
2. Still you want to click on hidden button. You may be try with JavaScript executor as given below.
Webelement eleLogout=driver.find element(By.class name("switch-btn"));
JavaScriptExecutor js=(JavaScriptExecutor)driver;
js.executeScript("arguments [0].click()",eleLogout);`
Try clicking on the link.
Use selectors like:
xpath:
//a[contains(#href, 'logout')] or
//*[#class='header-logout-btn']/a[contains(#href, 'logout')]
css:
a[href*=logout] or
.header-logout-btn a[href*=logout]

How to use Button class In Selenium Webdriver by the help of Java

Here is my code:
<div class="meal_buton_cont">
<button class="pink_button" data-target="#myModal" data-toggle="modal">Add Selected to Shopping List</button>
<button id="add_meal" class="gray_button">+ Add Meal</button>
</div>
I am using this:
driver.findElement(By.className("pink_button")).click();
But no popup open but some time open but not item take, it show blank.
How to create script for clicking Pink_button then my popup will be open?
Please help me
Few things to consider..Wait before the element appears using explicit waits like WebDriverWait API. Once the element is clickable using ExpectedConditions.elementToBeclickable(WebElement) then click on it and again wait till the pop-up appears. if its alert then use ExpectedConditions.alsertIsPresent() with WebDriverWait

Categories

Resources