How to switch to Md-dialog in selenium? - java

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.

Related

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

Selenium Webdriver with Java - Click on an hyperlinked Dropdown Container

I need to click on an element inside a Dropdown container. I've tried several searchs but I haven't been able to find the correct solution. The select method doesn't work, and I still don't know how to work with Selectors when there's no ID, Name or Class related to it. Here's the HTML code:
Account<span class="caret"></span>
<div class="account-dropdown__container">
<ul>
<li>Account</li>
<li>Invite Friends</li>
<li>Zola Store Credit</li>
<li>Registry Settings</li>
<li>Orders You've Placed</li>
<li><a>Log out</a></li>
</ul>
</div>
The first piece of code is a button, but if I put my mouse over it, it will show the Dropdown container that I am talking about. If I put my mouse over it without clicking, it will show the list of the Dropdown Container. (And I would also like to know how to hover an element to show the list without clicking it, because its hidden).
My question is, then: how can I click on Registry Settings?
It doesn't have an ID, nor a class (although it is inside the class account-dropdown__container). I think I can use By.name("Registry Settings"), but since is not visible unless the Dropdown list is open, it won't click and it will show Css Selector not found error. Care to help? Thanks!
Also, I am using Cucumber + Selenium + Java in IntelliJ IDEA, the synthaxis changes just a bit, but it is still different from the codes I tend to find in this forum. Hence, why I am asking for a specific solution to my problem.
You have to make the dropdown visible first.
As in Selenium you can't just hover an element, you will have to do it all in one go.
Check this: How to perform mouseover function in Selenium WebDriver using Java?
Actions action = new Actions(webdriver);
WebElement button = webdriver.findElement(By.class("account-link"));
action.moveToElement(button).moveToElement(webdriver.findElement(By.linkText("Registry Settings")).click().build().perform();
You may have to wait in between for the dropdown to appear. I have not tested the code, you will probably have to fix it before it works.
As you have mentioned when you put your mouse over to a button, it will show the Dropdown container.
Same can be automated with the help of selenium like this : (I am assuming account is a button )
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.linkText("Account"))).build().perform();
Now your drop-down is expanded or visible in UI, and you want to click on Registry Settings . Since your drop down is not made using Select options tags which are available in HTML. You can't use Select class from Selenium.
You will have to store every elements which are present in drop down to a list. and then based on some condition , you can click on your desire element.
Code:
List<WebElement> options = driver.findElements(By.cssSelector("div.account-dropdown__container ul li"));
for(WebElement option : options) {
if(option.getText().trim().contains("Registry Settings")) {
option.click();
}
}
Hope this will help.

How can I select this button?

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!

Java - Selenium Firefox Driver - cannot click on specific button in modal

I want my program to log into indeed.ca (this is working, as long as you enter correct user credentials), navigate to a specific job posting(working), click on the first orange apply button(working), a modal pops up.
Then I want to click on the blue apply button in modal that appears. This is not working. I have commented out my attempt at this portion of the program.
Any help would be much appreciated.
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Testing {
public static void main(String[] args) throws IOException {
//enter location of gecko driver
System.setProperty("webdriver.gecko.driver", "C:\\Users\\Padoga\\Documents\\geckodriver-v0.18.0-win64\\geckodriver.exe");
FirefoxDriver driver = new FirefoxDriver();
//login works correctly (given that you use proper credentials)
driver.get("https://secure.indeed.com/account/login?service=my&hl=en_CA&co=CA");
driver.findElement(By.xpath("//*[#id=\"signin_email\"]")).sendKeys("abc#gmail.com");
driver.findElement(By.xpath("//*[#id=\"signin_password\"]")).sendKeys("enterPassword");
driver.findElement(By.xpath("//*[#id=\"loginform\"]/button")).click();
//once logged in navigate to specific job
driver.navigate().to("https://ca.indeed.com/cmp/KGHM-International-Ltd./jobs/Financial-Analyst-7a08f1634e7d5c5c");
//clicking on first apply button(orange button) works correctly
Thread.sleep(3000);
driver.findElement(By.xpath("//*[#id=\"apply-state-picker-container\"]/div[1]/span[1]")).click();
//below not working, trying to click on apply button(blue apply button) in popup modal
//I've tried so many different xpaths and ids none seem to be triggering the apply button in modal
Thread.sleep(3000);
driver.switchTo().frame("indeedapply-modal-preload-iframe");
driver.findElement(By.id("apply")).click();
}
}
And here is the various html / javascript? that I have been trying to click on
i.e used as By.id, By.xpath, or By.className, none are working
The below code does not show up when I inspect the page source, only when I inspect the blue apply button in the modal that pops up after clicking the orange apply button, do I see the below code:
<div class="button_outter" id="apply-div">
<div class="button_inner">
<input class="button_content" id="apply" type="submit" name="apply" value="Apply">
</div>
</div>
I have tried using the switch to Iframe, but this is not working in this case. Can you check the below sendkeys approach after clicking the first apply button.
Actions act = new Actions(driver);
act.sendKeys(Keys.TAB, Keys.TAB, Keys.TAB, Keys.TAB, Keys.ENTER);
or
driver.findElement(By.xpath("//body")).sendKeys(Keys.TAB, Keys.TAB,Keys.TAB, Keys.TAB, Keys.ENTER);
Update: It seems, second recommendation actually worked for this case.
Hope this helps. Thanks.
You need to switch to the iframe firtst then call click() method on the Blue Apply button as follows:
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(#src,'https://apply.indeed.com/indeedapply/resumeapply')]")));
//perfrom other actions
//finally click on Blue Apply button
driver.findElement(By.xpath("//input[#id='apply']")).click();
It seems the index order of the iframes is backwards, at least that's what it looks like to me. I was able to click the "Blue Apply Button" using the following java code:
driver.get("https://ca.indeed.com/cmp/KGHM-International-Ltd./jobs/Financial-Analyst-7a08f1634e7d5c5c");
wait = new WebDriverWait(driver, 10);
//on my screen I had to scroll down to the orange apply button
WebElement applyButton = wait.until(ExpectedConditions.presenceOfElementLocated(By.className("indeed-apply-button")));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", applyButton);
applyButton.click();
Thread.sleep(1000);
driver.switchTo().frame(1);
driver.switchTo().frame(0);
driver.findElement(By.id("applicant.name")).sendKeys("My First Name is");
driver.findElement(By.id("apply-div")).click();

Can't click on customised checkbox on Chrome and IE11

I have to click on customised checkbox, it's inside a div like the code following:
<div class="checkbox input-group">
<input id="checkboxH" class="checkboxClass" type="checkbox" name="checkboxH">
<label class="required ng-scope" for="checkboxH">
Text of checkbox
</label>
</div>
I use label as element to be clicked, and it works on FF as code below
webDriver.findElement(By.xpath("//label[#for='checkboxH']")).click();
but it doesn't work on Chrome (latest version) and IE11, the error is:
org.openqa.selenium.WebDriverException: unknown error: Element is not clickable at point (634, 498). Other element would receive the click:
Note that the checkbox is not visible on the screen, it has to be scrolled into view. But with the same code, it works with FF. FF can click on checkbox even when it is not in the view.
I tried another way, like this:
WebElement a = webDriver.findElement(By.xpath("//label[#for='checkboxH']"));
a.sendKeys(Keys.ENTER);
a.click();
And it also works with FF, but not Chrome and IE. It seems, selenium with FF automatically scrolls element into view and performs click on it
I am stuck here with Chrome and IE. Any help much appreciated. Thanks
EDIT
Apparently, I fixed issue with IE by setting this capability, and it works for IE:
capability.setCapability("nativeEvents", false);
With Chrome, I don't know which capacities could fix this issue
I have a work around: I use Actions to move to element, then I use sleep for 2 second, then it works.

Categories

Resources