Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Selenium WebDriver中的sendKeys()_Java_Selenium_Selenium Webdriver - Fatal编程技术网

Java Selenium WebDriver中的sendKeys()

Java Selenium WebDriver中的sendKeys(),java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我是Selenium的新手,我正在尝试使用WebDriver上传一个文件。在这里,我尝试使用dom元素单击浏览按钮,如下所示: selenium.type("document.forms['UploadForm'].elements['browsebutton']",file.getAbsolutePath()); 但由于这种方法不起作用,我尝试使用WebDriver元素点击浏览按钮,如下所示:如何将dom元素更改为xpath或css选择器,如下所示 driver.findElement(By

我是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]";
   String upload="(//input[@id='content' and @value='requestFile'])[1]";
    String button="(//input[@id='content' and @value='process'])[1]";
我也试过使用id:不工作

String upload="(//input[@name='bulkUnBlockUploadForm' and @value='requestFile'])[2]";
String button="(//input[@name='bulkUnBlockUploadForm' and @value='process'])[2]";
   String upload="(//input[@id='content' and @value='requestFile'])[1]";
    String button="(//input[@id='content' and @value='process'])[1]";

问题是在我的jsp中,我有两个具有相同id和相同值但形式不同的浏览按钮。我有两个提交按钮,每个浏览按钮具有相同id和相同值但形式不同。因此,当我使用上述方法时,如果您有两个具有相同属性的按钮,它会同时点击两个提交按钮,然后重命名它们以便更好地访问(例如,通过给它们一个唯一的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();

这可以上传一个文件,这是我的工作

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

嗨,当我是初学者时,我也发现了同样的问题,有人告诉我你不能处理windows控件,所以使用第三方应用程序,如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应用程序并询问我

如果您是Selenium新手,请抓住机会使用WebDriver。你现在使用的硒只是一个不死生物!对于不同的元素,您不应该使用相同的
id
。确保
id
是唯一且有意义的,然后使用
driver.findElement(By.id())
.Dominik可以轻松访问元素。根据我的测试用例,我不想单击2浏览按钮。我尝试了第一个匹配的按钮,但没有得到解决方案。我已经编辑了我的问题并在那里添加了尝试过的解决方案。你可以参考它