Reputation: 642
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
Upvotes: 0
Views: 1199
Reputation: 3004
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.
Upvotes: 1
Reputation: 50949
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();
Upvotes: 2