简体   繁体   中英

Selenium: select overlay input type file

I'm having some problems with Selenium.
I have this overlay div , which contains an input type file and I'm trying to select it:

<div class="xenOverlay attachmentUploader"
    style="z-index: 9999; top: 92.7px; left: 206px; position: fixed; display: block;">

    <form action="http://localhost/xenforo/index.php?attachments/do-upload" method="post" enctype="multipart/form-data"
        class="formOverlay AutoInlineUploader AttachmentUploadForm NoAutoHeader" data-overlayclass="attachmentUploader">

        <dl class="ctrlUnit">
            <dt><label for="ctrl_upload">Upload a File (<span title="1,048,576 bytes">Max 1 MB</span>):</label></dt>
            <dd id="SWFUploadContainer" data-placeholder="#SWFUploadPlaceHolder" data-trigger="#ctrl_upload">
                <span id="SWFUploadPlaceHolder"></span>
                <input type="file" name="upload" class="textCtrl" onchange="this.blur()" id="ctrl_upload">
                <input type="reset" value="Close" class="OverlayCloser button smallButton">
            </dd>
        </dl>

        <div class="attachmentConstraints pairsRows">
            <dl>
                <dt>Accepted file types:</dt>
                <dd>zip, txt, pdf, png, jpg, jpeg, jpe, gif, torrent</dd>
            </dl>
        </div>

        <input type="hidden" name="_xfToken" value="1,1482434233,ccadf23a674c4c4b9def910cf5d55cea4b44ab2a">
        <input type="hidden" name="hash" value="a9288bd75c0c87638d9057237511e16c">
        <input type="hidden" name="content_type" value="post">
        <input type="hidden" name="key" value="">
        <input type="hidden" name="content_data[node_id]" value="3">
    </form>
</div>

I've tried selecting the input type file by id and by xpath but eclipse only gives me errors.

WebElement elem = driver.findElement(By.xpath("//*[@id='ctrl_upload']"));
elem.sendKeys("C://e.txt");

I'm using Selenium 3.0.1 with ChromeDriver on Windows 10

UPDATE:

Full code:

static void createNewThread(WebDriver driver, String category, String title, String message){       
    String url = "http://localhost/xenforo/index.php?forums/" + category + "/create-thread";
    driver.get(url);

    WebElement ttl = driver.findElement(By.className("titleBOT"));
    ttl.clear();
    ttl.sendKeys(title);

    WebElement msg = driver.findElement(By.xpath("//*[@id='ThreadCreate']/fieldset[1]/dl[2]/dd/div/div/iframe"));
    msg.click();
    msg.clear();
    msg.sendKeys(message); 

    driver.findElement(By.xpath("//*[@id='ctrl_uploader']")).click();   

    WebElement elem = driver.findElement(By.xpath("//input[@type='file']"));
    elem.sendKeys("C:\\e.txt");

    //driver.findElement(By.xpath("//*[@id='ThreadCreate']/dl[2]/dd/input[1]")).click();    
}

I think it doesn't finds the element that I'm searching. This element appears only when I click a button, and overlays the old content.

UPDATE 2:

I think I've found the problem. The Upload is via flash, not via html, when I click the flash button via click() the website doesn't reacts the same as with a real user click.

Try following:

WebElement elem = driver.findElement(By.xpath("//input[@type='file']"));
elem.sendKeys("C://e.txt");

Let me know, whether it works.

Try this simple way Hope it will work-

driver.findElement(By.id("ctrl_upload")).sendKeys("C:\\\\e.txt");

and do correct your file path C://e.txt to C:\\\\e.txt

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