0

Code trials:

package modules;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.LocalFileDetector;
import org.openqa.selenium.support.ui.Select;

public class PostUpdating {
    public void Execute()
    {
        WebElement w = LaunchApplication.driver.findElement(By.id("whats-new"));
        w.sendKeys("/C:/Users/Public/Pictures/Sample Pictures/Desert.jpg");
        Select sel = new Select(LaunchApplication.driver.findElement(By.id("rtSelectPrivacy")));
        sel.selectByVisibleText("Public");
        Select sel1 = new Select(LaunchApplication.driver.findElement(By.id("whats-new-post-in")));
        sel1.selectByVisibleText("My Profile");
        LaunchApplication.driver.findElement(By.id("aw-whats-new-submit")).click();
    }
}

The html

<div id="whats-new-textarea" style="position: relative;">
<textarea id="whats-new" class="bp-suggestions linkBox" rows="10" cols="50" name="whats-new" style="display: inline-block; height: 50px;"></textarea>
<div id="rtm-drop-files-title">Drop files here</div>
<div class="rtmp-url-scrapper-container">
<img class="rtmp-url-scrapper-loading" src="http://demo.rtcamp.com/rtmedia/wp-content/plugins/buddypress-media/app/assets/admin/img/boxspinner.gif">
<table id="rtmp-url-scrapper" style="display: none;">
<tbody>
   <tr>
      <td>
         <table id="rtmp-url-scrapper-img-holder">
            <tbody>
               <tr>
                  <td style="height:100px; overflow:hidden;" colspan="2">
                     <div id="rtmp-url-scrapper-img">
                        <img src="">
                     </div>
                  </td>
               </tr>
               <tr>
                  <td id="" style="width:50%; text-align:right;">
                     <input id="rtmp-url-prevPicButton" type="button" value="<">
                  </td>
                  <td id="" style="width:50%; text-align:left;">
                     <input id="rtmp-url-nextPicButton" type="button" value=">">
                  </td>
               </tr>
               <tr>
                  <td colspan="2">
                     <div id="rtmp-url-scrapper-img-count"></div>
                  </td>
               </tr>
            </tbody>
         </table>
      </td>
      <td>   

I want to insert a image into my text box but when I use Sendkeys and gave the path from my computer but instead of sending the image it is sending the path only into my text box.Also there is a button in my webpage for uploading the picture but when I clicked on button windows dialoge box open. So selenium can't deal with Windows GUI.So Any help how to do this?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Ab123
  • 415
  • 2
  • 13
  • 34

2 Answers2

0
drv.find_element_by_id("IdOfInputTypeFile").send_keys(os.getcwd()+"/image.png")
Dharman
  • 30,962
  • 25
  • 85
  • 135
Asgar
  • 431
  • 6
  • 20
0

To insert an image into the text box you need to send the filename along with the absolute path as a character sequence to the element inducing WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("textarea.bp-suggestions.linkBox#whats-new"))).sendKeys("/C:/Users/Public/Pictures/Sample Pictures/Desert.jpg");
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//textarea[@class='bp-suggestions linkBox' and @id='whats-new']"))).sendKeys("/C:/Users/Public/Pictures/Sample Pictures/Desert.jpg");
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352