2016-10-09 113 views
0

我需要点击下面的网页上的“浏览”按钮。无法点击使用硒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

OS: Win10 64位

的Java: 1.8

+1

使用异常日志更新您的故障单。另外,让我们知道你为什么试图点击该按钮并执行'AutoIT'脚本,而不是发送路径到文件''输入[@ id =“theFile_property(Resume)_1”]' – Andersson

+0

您能否在这里分享相关的HTML好 –

回答

2

形式(和浏览按钮)里面,你需要切换到其第一

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

并切换回

driver.switchTo().defaultContent(); 
1

使用下面的代码点击浏览:

//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(); 

希望它能帮助你。