I want to click on the Upload button but when the script runs this click event triggers a new browser to open which I cannot simulate when I try to test it manually.
I deliberately click on Start link (in the code) hoping that this new window opens only the first time and can be handled then and there, and thereafter I can concentrate on the click event of the Upload button in the parent window. But once again it brings up the new browser window.
How should I handle this?
driver.get("http://s000.tinyupload.com/index.php");
driver.manage().window().maximize();
driver.findElement(By.xpath("/html/body/table/tbody/tr[3]/td/div/table/tbody/tr/td[2]/span/a[1]/b")).click(); //Start link
Thread.sleep(4000);
Set<String> handles = driver.getWindowHandles();
String firstWindowHandle = driver.getWindowHandle();
handles.remove(firstWindowHandle);
String winHandle = handles.iterator().next();
if (winHandle != firstWindowHandle) {
String secondWindowHandle = winHandle;
driver.switchTo().window(secondWindowHandle);
driver.close();
driver.switchTo().window(firstWindowHandle);
}
driver.findElement(By.cssSelector("input[class=pole_plik]")).sendKeys(path);
driver.findElement(By.cssSelector("img[alt=Upload]")).click(); //upload button
1 Answer 1
Use following code, It works Tested it.
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://s000.tinyupload.com/index.php");
driver.manage().window().maximize();
driver.switchTo().defaultContent();
driver.findElement(By.cssSelector("input[class=pole_plik]")).sendKeys("/Users/jeevanbhushetty/Desktop/dock.jpg");
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("check_recipient('s000.tinyupload.com');");
-
It does. Thanks! How did you arrive at this method?ilm– ilm2016年02月17日 11:35:52 +00:00Commented Feb 17, 2016 at 11:35
-
Your welcome,
check_recipient()
function is written in your HTML markup, which gets executed on click of upload button.Jeevan Bhushetty– Jeevan Bhushetty2016年02月17日 11:48:13 +00:00Commented Feb 17, 2016 at 11:48 -
So... because check_recipient() is inside a script tag you decided to use JavascriptExecutor? I need a bit more help to understand this concept.ilm– ilm2016年02月17日 12:09:47 +00:00Commented Feb 17, 2016 at 12:09
-
1Being frank, code written in '<script>' tag
function check_recipient(file_server_domain) { postIt(file_server_domain); }
is of no use it is just calling another function in other script, you can directly call the function itself. In your script if you replacecheck_recipient('s000.tinyupload.com');
withpostIt('s000.tinyupload.com');
script will still work. Just one additional call is made which is of no use. Coming back to your question, usingJavascriptExecutor
class of selenium you can execute any javascript.Jeevan Bhushetty– Jeevan Bhushetty2016年02月17日 12:25:52 +00:00Commented Feb 17, 2016 at 12:25 -
why doesn't executor.executeScript("arguments[0].click();", element); work?ilm– ilm2016年02月19日 06:48:19 +00:00Commented Feb 19, 2016 at 6:48