selenium java load image in for xlink:href attribute



I am trying to load images in one website, but I didn’t succeed,

This is the Source Code when pressing F12

    <div class="styles_slide__1WxkV">
    <div class="styles_item__2z_U9 styles_btnLoaded__3cD1S" data-position="1">
    <span class="styles_imageAdded__nbDDD">
    <svg viewBox="0 0 24 24" class="sc-bdVaJa src___StyledBox-fochin-0 dCYVFu">
    <title>conteneur de l'image</title>
    <use xlink:href="#SvgCameraoutline"></use>
    </svg>
    </span><span class="_2QIe0 _1rwR5 _1pJw9 _137P- P4PEa _35DXM">Photo n°1</span>
    </div>
    </div>

However I tried many codes :

     driver.findElement(By.cssSelector("div[id='pictures'] div:nth-child(3) div:nth-child(1) span:nth-child(2)"));
        Actions act2=new Actions(driver);
        act2.moveToElement(element).sendKeys("C:\url\test.jpg").perform();

 - 



    driver.findElement(By.cssSelector("div[id='pictures'] div:nth-child(3) div:nth-child(1) span:nth-child(2)")).sendKeys("C:\url\test.jpg");
       
- 



    driver.findElement(By.cssSelector("body > div:nth-child(3) > div:nth-child(1) > section:nth-child(1) > main:nth-child(1) > div:nth-child(1) > div:nth-child(1) > section:nth-child(3) > div:nth-child(1) > div:nth-child(1) > div:nth-child(7) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > span:nth-child(3) > div:nth-child(1) > div:nth-child(3) > div:nth-child(1) > span:nth-child(1) > svg:nth-child(1)")).sendKeys("C:\url\sac.jpg");

- 



   

     driver.findElement(By.xpath("//div[contains(@class, 'styles_item__2z_U9')]")).sendKeys("C:\url\sac.jpg");

-



    driver.findElement(By.xpath("//*[name()='use' and @*='#SvgCameraoutline']")).sendKeys("C:\url\sac.jpg");

-



    driver.findElement(By.xpath("//svg[contains(@class, 'sc-bdVaJa')]")).sendKeys("C:\url\sac.jpg");

But anyone of them is working with, can you help me please, This is the code source extract of the webpage which I would like to automate by loading images in it.

I am trying to automate one stuff boring (add a post) in this website (url removed)

Then click on “deposer-une-annonce” and follow steps until come to one step to load images, in this step I can’t load image automatically by using selenium java as I explained above

Answer

I see one input tag that has type as file.

But I couldn’t figure out your website, so I could not enable that input field.

But I believe if you send_keys to Quel est le titre de l’annonce ? with some other input, it may help you past this issue.

Anyway to upload an image, you can use the below css selector :-

input[type='file']

and in code :

driver.findElement(By.cssSelector("input[type='file']")).sendKeys("C:\url\test.jpg");
   

or with Explicit waits :

new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@type='file']"))).sendKeys("C:\url\test.jpg");


Source: stackoverflow