At the end, I am not able to click on save button. The last click is not working for me.
System.setProperty("webdriver.chrome.driver" , "C:\\Users\\gausia.fatima\\Downloads\\automation\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.idfystaging.com/users/sign_in");
driver.findElement(By.name("user[login]")).sendKeys("themanager#mailinator.com");
driver.findElement(By.name("user[password]")).sendKeys("Password123");
driver.findElement(By.name("commit")).click();
driver.get("http://www.idfystaging.com/profiles/new?company_id=189");
Select mydrpdwn = new Select(driver.findElement(By.id("company_id")));
mydrpdwn.selectByVisibleText("Fringe A Division");
driver.findElement(By.id("email_addresses")).sendKeys("awesome1#mailinator.com");
driver.findElement(By.id("counts_aadhaar")).sendKeys("1");
//WebDriverWait wait = new WebDriverWait(driver,30);
//wait.until(ExpectedConditions.presenceOfElementLocated(By.name("commit")));
driver.findElement(By.name("commit")).submit();
System.out.println("candiate is invited.");
//driver.close();
In the "Invite Candidates" page, you have to submit button with the same name as "commit", so selenium is throwing error.
http://www.idfystaging.com/profiles/new?company_id=189
Use the CssSelectorto get the control and submit them.
Java code un tested but it should work.
driver.findElement(By.cssSelector("input.f21")).Submit();
Note : This is c# code and I tested
driver.FindElement(By.CssSelector("input.f21")).Submit();
as name=commit returns two elements in the page, should use more strict selector to find the Invite button.
Try the following code to click on Invite button:
driver.findElement(By.xpath("//input[#value='Invite']")).click();
or
driver.findElement(By.xpath("//input[#value='Invite']")).submit();
You can use following code. I have modified it and it is working fine. Please check if it is working fo you.
System.setProperty("webdriver.chrome.driver" , "C:\\Users\\gausia.fatima\\Downloads\\automation\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.idfystaging.com/users/sign_in");
driver.findElement(By.id("focus_on_load")).sendKeys("themanager#mailinator.com");
driver.findElement(By.id("user_password")).sendKeys("Password123");
driver.findElement(By.name("commit")).click();
Thread.sleep(5000);
driver.navigate().to("http://www.idfystaging.com/profiles/new?company_id=189");
Select mydrpdwn = new Select(driver.findElement(By.id("company_id")));
mydrpdwn.selectByVisibleText("Fringe A Division");
driver.findElement(By.id("email_addresses")).sendKeys("awesome1#mailinator.com");
driver.findElement(By.id("counts_aadhaar")).sendKeys("1");
driver.findElement(By.cssSelector("input.f21")).click();
System.out.println("candiate is invited.");
Related
The same being opened in an brower without automating is opening in english.Using the below Code:
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.get("https://www.facebook.com/r.php?locale=EN_US&campaign_id=973072070&extra_1=s%7Cc%7C256741383347%7Ce%7Cfacebook%20sign%20up%7C&placement=&creative=256741383347&keyword=facebook%20sign%20up&partner_id=googlesem&extra_2=campaignid%3D973072070%26adgroupid%3D54006292691%26matchtype%3De%26network%3Dg%26source%3Dnotmobile%26search_or_content%3Ds%26device%3Dc%26devicemodel%3D%26adposition%3D%26target%3D%26targetid%3Dkwd-295862466660%26loc_physical_ms%3D20464%26loc_interest_ms%3D%26feeditemid%3D19894516786%26param1%3D%26param2%3D&gclid=EAIaIQobChMIhae5qNH98AIVtdxMAh18Rw2UEAAYASABEgJvEvD_BwE");
Thread.sleep(5000);
WebElement day = driver.findElement(By.id("day"));
WebElement month = driver.findElement(By.id("month"));
WebElement year = driver.findElement(By.id("year"));
Select select = new Select(day);
select.selectByVisibleText("5");
Select select1 = new Select(month);
select.selectByVisibleText("April");
Select select2 = new Select(year);
select.selectByVisibleText("1995");
I don't know why the site is opening in Hindi, that doesn't happen for me. In any case, I guess you can just tell selenium to switch language by identifying and clicking the right language link. I assume you want English?
// code to open the page
driver.findElement(By.xpath("//*[text() = 'English (US)']")).click()
//rest of your test
Just change your facebook link instead of "http://facebook.com" use "http://en-gb.facebook.com
You can set the ChromeDriver locale with the following code:
ChromeOptions options = new ChromeOptions();
options.addArguments("--lang=en-US");
ChromeDriver driver = new ChromeDriver(options);
This is website: https://html5demos.com/drag/
And this is my code perform drag and drop element using java, chrome and selenium 3.141.59
By dustBin = By.xpath("//div[#id=\"bin\"]");
By one = By.xpath("//a[#id=\"one\"]");
WebElement From = driver.findElement(one);
WebElement To = driver.findElement(dustBin);
Actions actions = new Actions(driver);
actions.dragAndDrop(From,To).build().perform();
actions.clickAndHold(From).moveToElement(To).build().perform();
actions.dragAndDropBy(From,176,260).build().perform();
When I run test with TestNG the element is not moving to dustBin. So I dont know why. Any one help me resolve this issue. Thank you.
You should use findElementById to find locator instead of xpatch.
Try this one:
WebElement sourceLocator = driver.findElement(By.id("one"));
//To get target locator
WebElement targetLocator = driver.findElement(By.id("bin"));
I'm testing a GWT + SMARTGWT application like Paint and I'm trying to locate the elements of this web application using Selenium Webdriver. The method which I have used to locate the elements is by the relative XPath of those elements but the problem which I am currently facing is that this method is working correctly on the browsers like Chrome, Firefox, and Edge but not on the IE browser. The version of IE on my PC is 11.1593.14393.0. In the IE browser, this relative XPath method is giving a TimeOutException. I have given the explicit wait:
wait.until(ExpectedConditions.elementToBeClickable(webelement));
The IE browser is not able to find the element. I am also getting the following exception sometimes for other elements:
Exception in thread "main" org.openqa.selenium.InvalidSelectorException: Unable to locate an element with the xpath expression //img[contains(#src,'Insert XXX'] because of the following error:
Error: Bad token: ]
Among the troubleshooting solutions to this issue, I tried enabling/disabling the protected mode in IE for all the levels but this method didn't work. Along with that, I also tried checking the box next to the option - "Allow active content to run files on My Computer" but this method also failed to work.
What should I do to fix my issue?
This is my code. Here firstly, I will click on the Insert button located on the top bar of the application and after clicking on the Insert button, a window will launch on which I will click on the Close button.
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.ie.driver", "D:\\SELENIUM\\Drivers\\iedriverserver.exe");
WebDriver driver = new InternetExplorerDriver();
Thread.sleep(3000);
driver.get(baseURL);
WebDriverWait wait = new WebDriverWait(driver, 10);
final String InsertPath = "//img[contains(#src,'Insert XXXX')]";
final String closePath="//img[contains(#src,'close')]";
WebElement Insert = driver.findElement(By.xpath(InsertPath));
wait.until(ExpectedConditions.elementToBeClickable(Insert));
Thread.sleep(2000);
Insert.click();
WebElement close = driver.findElement(By.xpath(closePath));
wait.until(ExpectedConditions.elementToBeClickable(close));
Thread.sleep(3000);
close.click();
}
}
Edit: I also used finding the element using Javascript executor in my code as follows:
WebElement Insert = driver.findElement(By.xpath(InsertPath));
Thread.sleep(2000);
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("arguments[0].click();", Insert);
Sadly, this method also failed to work in the IE browser.
So, I was able to locate the elements by using the latest driver of the Internet Explorer and giving the following desired capabilities in my code to the IE browser.
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability("requireWindowFocus", true);
ieCapabilities.setCapability("unexpectedAlertBehaviour", "accept");
ieCapabilities.setCapability("ignoreProtectedModeSettings", true);
ieCapabilities.setCapability("disable-popup-blocking", true);
ieCapabilities.setCapability("enablePersistentHover", true);*/
System.setProperty("webdriver.ie.driver", "D:\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver(ieCapabilities);
I am checking whether or not a page appears using Selenium. When I click the page, however, a printer print prompt appears (like the window that says select printer and such). How can I have Selenium close this window by hitting cancel?
I tried looking to alerts, but it seems like those will not work since the print window is a system prompt. It does not recognize any alerts appearing.
The most recent I tried using is by just sending keys like tab and enter in order to have the cancel button selected, however, it doesn't recognize any keys as being pressed.
How can I handle this case?
public static boolean printButton() throws Exception {
WebDriver driver = new FirefoxDriver();
driver.get("website");
try {
Thread.sleep(3000);
WebElement temp = driver.findElement(By.xpath("//*[#id='block-print-ui-print-links']/div/span/a"));
temp.click();
Actions action = new Actions(driver);
action.sendKeys(Keys.TAB).sendKeys(Keys.ENTER);
Thread.sleep(6000);
}
catch (Exception e) {
System.out.println("No button.");
driver.close();
return false;
}
I would simply disable the print dialog by overriding the print method :
((JavascriptExecutor)driver).executeScript("window.print=function(){};");
But if you goal is to test that the printing is called then :
// get the print button
WebElement print_button = driver.findElement(By.cssSelector("..."));
// click on the print button and wait for print to be called
driver.manage().timeouts().setScriptTimeout(20, TimeUnit.SECONDS);
((JavascriptExecutor)driver).executeAsyncScript(
"var callback = arguments[1];" +
"window.print = function(){callback();};" +
"arguments[0].click();"
, print_button);
If you are going for testing only Chrome browser here is mine solution. Because of 'Robot' class or disabling print didn't work for my case.
// Choosing the second window which is the print dialog.
// Switching to opened window of print dialog.
driver.switchTo().window(driver.getWindowHandles().toArray()[1].toString());
// Runs javascript code for cancelling print operation.
// This code only executes for Chrome browsers.
JavascriptExecutor executor = (JavascriptExecutor) driver.getWebDriver();
executor.executeScript("document.getElementsByClassName('cancel')[0].click();");
// Switches to main window after print dialog operation.
driver.switchTo().window(driver.getWindowHandles().toArray()[0].toString());
Edit: In Chrome 71 this doesn't seem to work anymore since the script can't find the Cancel button. I could make it work by changing the line to:
executor.executeScript("document.querySelector(\"print-preview-app\").shadowRoot.querySelector(\"print-preview-header\").shadowRoot.querySelector(\"paper-button.cancel-button\").click();");
Actually you can't handle windows (OS) dialogs inside Selenium WebDriver.
This what the selenium team answers here
The current team position is that the print dialog is out of scope for
the project. WebDriver/Selenium is focused on emulating a user's
interaction with the rendered content of a web page. Other aspects of
the browser including, but not limited to print dialogs, save dialogs,
and browser chrome, are all out of scope.
You can try different approach like AutoIt
we can also use key for handling the print or press the cancel button operation. and it works for me.
driver.switchTo().window(driver.getWindowHandles().toArray()[1].toString());
WebElement webElement = driver.findElement(By.tagName("body"));
webElement.sendKeys(Keys.TAB);
webElement.sendKeys(Keys.ENTER);
driver.switchTo().window(driver.getWindowHandles().toArray()[0].toString());
Native window based dialog can be handled by AutoItX as described in the following code
File file = new File("lib", jacobDllVersionToUse);
System.setProperty(LibraryLoader.JACOB_DLL_PATH, file.getAbsolutePath());
WebDriver driver = new FirefoxDriver();
driver.get("http://www.joecolantonio.com/SeleniumTestPage.html");
WebElement printButton = driver.findElement(By.id("printButton"));
printButton.click();
AutoItX x = new AutoItX();
x.winActivate("Print");
x.winWaitActive("Print");
x.controlClick("Print", "", "1058");
x.ControlSetText("Print", "", "1153", "50");
Thread.sleep(3000); //This was added just so you could see that the values did change.
x.controlClick("Print", "", "2");
Reference : http://www.joecolantonio.com/2014/07/21/selenium-how-to-handle-windows-based-dialogs-and-pop-ups/
Sometimes 2 different statements as above (webElement.sendKeys(Keys.TAB)
webElement.sendKeys(Keys.ENTER)) will not work, you can use with combination of Tab and Enter keys as below, This will close the Print preview window.
Using C# :
Driver.SwitchTo().Window(Driver.WindowHandles[1]);
IWebElement element = Driver.FindElement(By.TagName("body"));
element.SendKeys(Keys.Tab + Keys.Enter);
Driver.SwitchTo().Window(Driver.WindowHandles[0]);
Erçin Akçay answer updated.
// Choosing the second window which is the print dialog.
// Switching to opened window of print dialog.
driver.switchTo().window(driver.getWindowHandles().toArray()[1].toString());
// Runs javascript code for cancelling print operation.
// This code only executes for Chrome browsers.
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("document.querySelector(\"body > print-preview-app\").shadowRoot.querySelector(\"#sidebar\").shadowRoot.querySelector(\"print-preview-button-strip\").shadowRoot.querySelector(\"div > cr-button.cancel-button\").click();");
// Switches to main window after print dialog operation.
driver.switchTo().window(driver.getWindowHandles().toArray()[0].toString());
Is it possible to interact with Chrome from within a java application? Say for example fill in an input field and submit/trigger a button event?
Yes. See WebDriver's ChromeDriver.
Example usage taken from docs:
// Optional, if not specified, WebDriver will search your path for chromedriver.
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com/xhtml");
Thread.sleep(5000); // Let the user actually see something!
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("ChromeDriver");
searchBox.submit();
Thread.sleep(5000); // Let the user actually see something!
driver.quit();