我需要点击以下网页上的“浏览”按钮。
http://www.guru99.com/freelancing.html
我编写了以下代码但webdriver无法找到Browse按钮元素。请帮忙。
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();
}
}
我正在使用:
Firefox版本: 49.0.1
Selenium版本:版本3.0.0-beta4
操作系统: Win10 64位
Java: 1.8
答案 0 :(得分:2)
表单(和“浏览”按钮)位于<iframe>
内,您需要先切换到它
WebElement iframe = driver.findElement(By.cssSelector("[src*='recruit'"])); //locate the iframe
driver.switchTo().frame(iframe);
并切换回来
driver.switchTo().defaultContent();
答案 1 :(得分:1)
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.