I am automating a web application using Selenium webdriver(java). I have handled popup windows in other parts of application but i'm not able to handle showModalDialog window. Application has read only text box and I have to fill this by clicking Search button, which will open new popup window.
HTML code for this Textbox and Serach button is as follows.
<tr>
<td><label class="classLabel">Textbox<sup class="mandatory">*</sup></label></td>
<td><input type="text" name="textbox" value="" readonly="readonly" class="someTextbox">
<img src="/Some/url/search.gif" alt="Select Help" onclick="launch2()" />
</td>
</tr>
After clicking Search button, a showModalDialog window will open and there I have to click a radio button to fill text box.
This showModalDialog window has iframe element and below is html code for this.
<iframe src="/some/url.do?parameter=getData" width='100%' height='100%' border=0 >
</iframe>
Below is the code I'm using to switch into popup window and select iframe. Here, test case is simply stopping after clicking Search button and popup window also appearing but it is not handling popup.
driver.findElement(By.cssSelector("img[alt=\"Select Help\"]")).click();
String winHandleBefore = myD.getWindowHandle();
for(String winHandle : myD.getWindowHandles()){
driver.switchTo().window(winHandle);
}
driver.switchTo().frame(0);
driver.findElement(By.name("hier_data_id")).click();
If anybody have idea on how to handle showModalDialog windows, it would be really helpful. Suggestions would be appreciated.
Thanks in advance !
import java.util.Iterator;
import java.util.Set;
private static String popUp;
//get window handle before popup
Set<String> beforePopUp = driver.getWindowHandles();
//do the click
driver.findElement(By.cssSelector("img[alt=\"Select Help\"]")).click();
//get window handles after the click
Set<String> afterPopup = driver.getWindowHandles();
//remove all window handles before the popup
afterPopUp.removeAll(beforePopUp);
if(afterPopUp.size() == 1) {
popUp = (String) afterPopUp.toArray()[0];
} else {// there might be more than on popups that might have been triggered by the click...do some error handling here}
//switch to popUp
driver.switchTo().window(popUp);
//do your stuff
Related
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
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.
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();
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]
I have a condition where in ui:repeat on button click i'm setting the values into inputHidden using javascript. I want that to open in a popup window to edit. I'm trying to open show_dialog after setting values in javascript block .here's my code
<ui:repeat var="c" value="#{abc.getabcList()}" varStatus="status">
.................code.........
<h:commandButton value="Edit" onclick="setSelectedValuesForEdit('#{c.name}','#{c.address}'"></h:commandButton>
This is my javascript code.
function setSelectedValuesForEdit(nameValue,addressValue)
{
document.getElementById("abclist:name").value=nameValue;
document.getElementById("abclist:address").value=addressValue;
show_dialog('/xyz/faces/path.xhtml','Abc inputs',410,430);
}
And i have set inputHidden in page .And i'm able to see the values set to input hidden when i do viewsource.
But how to open this in popup window to edit..where i'm trying to open a Jquery show_dialog(.....)as in above code.