简体   繁体   中英

Upload image Selenium Webdriver Python - Unable upload image => Error: "Unable to locate element: {"method":"xpath","selector":""

I encountered an image upload problem on Selenium webdriver in Python, I inspected the HTML and used Full Xpath to locate the element but it didn't work. I still get this error selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[2]/div/div[4]/div[2]/div/div[2]/div/div/div 1 /div/table/tbody/tr/td/table/tbody/tr/td/div[3]/div"}

This is my code:

#step 2 upload photo
upload_photo = driver.find_element_by_xpath('/html/body/div[2]/div/div[4]/div[2]/div/div[2]/div/div/div[1]/div/table/tbody/tr/td/table/tbody/tr/td/div[3]/div')
upload_photo.send_keys("/Users/Documents/2020//CASINO/668X593.png")
time.sleep(2)

And this is the HTML snipet:

    <div class="ve-Fc-Ye-Qd-Jo">Drag photos and videos here</div>
    <div class="ve-Fc-Ye-Qd-Io">Or, if you prefer...</div>
    <div id=":f"><div role="button" class="a-b-c d-u d-u-F" tabindex="0" style="user-select: none;">Select photos and videos from your computer
    </div>
</div>

Screnshot of interface

Try the following.To get the div inside the div id=':f'.

upload_photo = driver.find_element_by_xpath("//*div[@id=':f']/div")
upload_photo.send_keys("/Users/Documents/2020/CASINO/668X593.png")

Try to find element by id:

upload_photo = driver.find_element_by_id(":f")

You can also use waits to solve following difficult : if an element is not yet present in the DOM.

See more here about it:

https://selenium-python.readthedocs.io/waits.html#:~:text=Selenium%20Webdriver%20provides%20two%20types,trying%20to%20locate%20an%20element .

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