上载图像Selenium Webdriver Python-无法上载图像=>错误:“无法定位元素:{“方法”:“xpath”,“选择器”:”
我在Python中的SeleniumWebDriver上遇到了一个图像上载问题,我检查了HTML并使用完整的Xpath来定位元素,但它不起作用。 我仍然收到这个错误selenium.common.exceptions.NoSuchElementException:Message:没有这样的元素:无法定位元素:{method:xpath,selector:/html/body/div[2]/div/div/div[4]/div[2]/div/div/div/div/table/tbody/tr/td/table/tbody/tr/td/div[3]/div} 这是我的代码:上载图像Selenium Webdriver Python-无法上载图像=>错误:“无法定位元素:{“方法”:“xpath”,“选择器”:”,python,selenium,selenium-webdriver,xpath,file-upload,Python,Selenium,Selenium Webdriver,Xpath,File Upload,我在Python中的SeleniumWebDriver上遇到了一个图像上载问题,我检查了HTML并使用完整的Xpath来定位元素,但它不起作用。 我仍然收到这个错误selenium.common.exceptions.NoSuchElementException:Message:没有这样的元素:无法定位元素:{method:xpath,selector:/html/body/div[2]/div/div/div[4]/div[2]/div/div/div/div/table/tbody/tr/t
#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 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>
尝试以下操作。要在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中还没有元素
更多信息请参见此处:
.共享html dom,这样我们就可以帮助您编写正确的xpath。我刚刚尝试过,又出现了一个错误:selenium.common.exceptions.InvalidSelectorException:Message:invalid selector:无法找到xpath表达式为/*div[@id=':f']的元素/div,因为以下错误:SyntaxError:未能对“文档”执行“评估”:字符串“//*div[@id=”:f']/div”不是有效的XPath表达式。