简体   繁体   中英

Unable to click on the “Browse” button using selenium webdriver

I need to click on the "Browse" button on the below webpage.

http://www.guru99.com/freelancing.html

I have written the below code but webdriver fails to find the Browse button element. Please help.

import java.io.IOException;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FileUpload {

    public static void main(String[] args) throws IOException {
        System.setProperty("webdriver.gecko.driver", "C:\\Eclipse\\Drivers\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.navigate().to("http://www.guru99.com/freelancing.html");
        driver.findElement(By.id("theFile_link(Resume)")).click();
        //Below line execute the AutoIT script
        Runtime.getRuntime().exec(System.getProperty("user.dir")+"\\FileUpload.exe");
        driver.close();
    }
}

I'm Using:

Firefox Version: 49.0.1

Selenium Version: Version 3.0.0-beta4

OS: Win10 64 bit

Java: 1.8

The form (and Browse button) is inside <iframe> , you need to switch to it first

WebElement iframe = driver.findElement(By.cssSelector("[src*='recruit'"])); //locate the iframe
driver.switchTo().frame(iframe);

And to switch back

driver.switchTo().defaultContent();

use the below code to click on browse:

//first switch to iframe as the browse button is inside the iframe.
WebElement iframe = driver.findElement(By.cssSelector("[src*='recruit'"])); 
driver.switchTo().frame(iframe);

//scroll into the browse button
WebElement element = driver.findElement(By.id("theFile_link(Resume)"));
((JavascriptExecutor)   driver).executeScript("arguments[0].scrollIntoView(true);", element);

//now click on browse button
element.click();

hope it will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM