简体   繁体   中英

sendKeys() in Selenium WebDriver

I am new to Selenium and I am trying to upload a file using WebDriver. Here I am trying to click browse button using dom element as follows:

selenium.type("document.forms['UploadForm'].elements['browsebutton']",file.getAbsolutePath());

But since the approach did not work am trying to hit browse button using WebDriver element as below: How can i change my dom element to xpath or css selector as below?

driver.findElement(By.cssSelector("input[type=\"file\"]")).click();

I cant write xpath as

selenium.click("xpath="//input[@name='uplaod' and @value='browsebutton']");

since i have multiple browse buttons with same name and value.. So i need to pick using dom element itself. How do i do it?

Thanks in advance for help.

Dominik i have tried using the below xpath since there is no name attribute:But not working

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

I tried using id as well:Not working

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

The issue is in my jsp,i have 2 browse buttons s with same id and same value,but different form.I have 2 submit buttons for each of browse buttons with same id and same value,but different forms.So when am using the above approaches its hitting both the submit buttons

This can upload a file, it is work for me.

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

If you have two buttons with equal attributes, then either rename them to be better accessible (for example by giving them a unique id) or try to change your XPath statement to something like this:

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

hi when iam beginner i also find a same issue someone told me you cant handle windows controls so use third party application such autoit , iam using 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 run your java application still if you find query ask me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM