我是Selenium的新手,我正在尝试使用WebDriver上传文件。在这里,我尝试使用dom元素点击浏览按钮,如下所示:
selenium.type("document.forms['UploadForm'].elements['browsebutton']",file.getAbsolutePath());但是由于这种方法不起作用,所以我尝试使用WebDriver元素点击浏览按钮,如下所示:如何将dom元素更改为xpath或css选择器?
driver.findElement(By.cssSelector("input[type=\"file\"]")).click();我不能将xpath写为
selenium.click("xpath="//input[@name='uplaod' and @value='browsebutton']");因为我有多个具有相同名称和值的浏览按钮。所以我需要选择使用dom元素本身。我该怎么做呢?
提前感谢您的帮助。
Dominik我尝试使用下面的xpath,因为没有name属性:但不起作用
String upload="(//input[@name='bulkUnBlockUploadForm' and @value='requestFile'])[2]";
String button="(//input[@name='bulkUnBlockUploadForm' and @value='process'])[2]";我也尝试使用id :不起作用。
String upload="(//input[@id='content' and @value='requestFile'])[1]";
String button="(//input[@id='content' and @value='process'])[1]";问题出在我的jsp中,我有两个浏览按钮,具有相同的id和相同的值,但不同的表单。我有两个提交按钮,每个浏览按钮具有相同的id和相同的值,但当我使用上面的方法时,它会同时点击两个提交按钮,但forms.So不同。
发布于 2012-10-29 19:53:49
这个可以上传一个文件,很适合我。
public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.freepdfconvert.com/");
driver.findElement(By.id("UploadedFile")).sendKeys("C:\\Users\\Reputa\\Downloads\\HP1.pdf");
try {
Thread.sleep(4000);
} catch (Exception e) {}
driver.findElement(By.name("pdfsubmit")).click();
}发布于 2012-10-25 14:05:30
如果您有两个具有相同属性的按钮,则可以重命名它们以便于访问(例如,通过为它们提供唯一的id),或者尝试将您的XPath语句更改为如下所示:
String uploadButton1 = "(//input[@name='upload' and @value='browsebutton'])[1]";
String uploadButton2 = "(//input[@name='upload' and @value='browsebutton'])[2]";
driver.findElement(By.xpath(uploadButton1)).click();
driver.findElement(By.xpath(uploadButton2)).click();发布于 2015-09-25 18:59:37
嗨,当我是初学者,我也发现了一个相同的问题,有人告诉我,你不能处理窗口控件,所以使用第三方应用程序,如autoit,我正在使用autoit。
1. download autoit.
2. no need of any jars just add Runtime,getruntime().execute('path of exe');in your code
3.code of file upload is below
Local $hWnd=WinWait("[CLASS:#32770]","",10)
ControlFocus($hWnd,"","Edit1")
Sleep(2000)
ControlSetText($hWnd, "", "Edit1", "path of file to upload")
Sleep(2000)
ControlClick($hWnd, "","Button1");4运行你的java应用程序,如果你发现了查询,请问我。
https://stackoverflow.com/questions/13062453
复制相似问题