整个html:https://dpaste.com/82768UN7D
有几个这样的元素:
<input type="file" name="images_responsiveimage_related-0-responsive_image" accept="image/*" id="id_images_responsiveimage_related-0-responsive_image"></p>
我想通过Python的Selenium:找到最后一个
last_input_type_xpath = "//input[@type='file'][last()]"
input_element = self.driver.find_element_by_xpath(xpath)
我得到NoSuchElementException。
您的xpath似乎是错误的。要获取最后一个输入文件,请尝试
(//input[@type='file'])[last()]
理想情况下,您应该等待元素使用webdriverwait
input_element=WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "(//input[@type='file'])[last()]")))
您需要导入以下库
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC