![](/img/trans.png)
[英]Unable to click the 'button' in selenium webdriver (using Java)
[英]Unable to click on the “Browse” button using selenium webdriver
我需要点击下面网页上的“浏览”按钮。
http://www.guru99.com/freelancing.html
我已经编写了以下代码,但Webdriver无法找到“浏览”按钮元素。 请帮忙。
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
硒版本:版本3.0.0-beta4
操作系统: Win10 64 bit
爪哇: 1.8
表单(和“浏览”按钮)位于<iframe>
内部,您需要先切换到该表单
WebElement iframe = driver.findElement(By.cssSelector("[src*='recruit'"])); //locate the iframe
driver.switchTo().frame(iframe);
并切换回
driver.switchTo().defaultContent();
使用以下代码单击浏览:
//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();
希望对您有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.