0

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

0

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');");
5
  • It does. Thanks! How did you arrive at this method?
    – ilm
    Commented 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. Commented 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
    Commented Feb 17, 2016 at 12:09
  • 1
    Being 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 replace check_recipient('s000.tinyupload.com'); with postIt('s000.tinyupload.com'); script will still work. Just one additional call is made which is of no use. Coming back to your question, using JavascriptExecutor class of selenium you can execute any javascript. Commented Feb 17, 2016 at 12:25
  • why doesn't executor.executeScript("arguments[0].click();", element); work?
    – ilm
    Commented Feb 19, 2016 at 6:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.