繁体   English   中英

上传图像 Selenium Webdriver Python - 无法上传图像 => 错误:“无法定位元素:{“方法”:“xpath”,“选择器”:“”

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

我在 Python 中的 Selenium webdriver 上遇到了图像上传问题,我检查了 HTML 并使用 Full Xpath 来定位元素,但它没有用。 我仍然收到此错误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"}

这是我的代码:

#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)

这是 HTML 片段:

    <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>

界面截图

尝试以下操作。要在 div id=':f' 中获取 div。

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

尝试通过 id 查找元素:

upload_photo = driver.find_element_by_id(":f")

您还可以使用等待来解决以下困难:如果元素尚未出现在 DOM 中。

在此处查看更多信息:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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