简体   繁体   中英

selenium driver, button=driver.find_element_by_xpath(“//input[@id='alpha']”) Message: no such element: Unable to locate element

i want to avoid the exception or error due to unfound button, and change a variable to stop the loop. i couldn't find on documentation what it really return in case of unexistant element

while there_is_more:
    button=driver.find_element_by_xpath("//input[@id='alpha']")

    if not button :
        there_is_more=False

The goal is to stop when the button is not found

while there_is_more:
    button=driver.find_elements_by_xpath("//input[@id='alpha']")

    if len(button)==0 :
        there_is_more=False

use find elements and check for the result list.

I tried this, il solved my problem perfectly

from selenium.common.exceptions import NoSuchElementException
    
    try:
        more_button=driver.find_element_by_xpath("//input[@id='alpha']")
        more_button.click()

    except NoSuchElementException:
        there_is_more= False

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