我有这段代码,如果元素存在,它将打印innerHTML
值:
def display_hotel(self):
for hotel in self.hotel_data:
if hotel.find_element(By.CSS_SELECTOR, 'span[class="_a11e76d75 _6b0bd403c"]'):
hotel_original_price = hotel.find_element(By.CSS_SELECTOR, 'span[class="_a11e76d75 _6b0bd403c"]')
hotel_original_price = hotel_original_price.get_attribute('innerHTML').strip().replace(' ', '')
print(f"Original:\t\t\t{hotel_original_price}")
当我继续并运行程序时,我收到一个错误
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"span[class="_a11e76d75 _6b0bd403c"]"}
我希望如果元素不存在,它应该一起跳过,为什么即使在一个块span[class="_a11e76d75 _6b0bd403c"]
下它仍然试图继续执行代码?if
我在这里错过了什么吗?