Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Exit while true循环在没有更多可供单击的情况下使用Python和Selenium退出_Python_Selenium_Web_While Loop_Browser Automation - Fatal编程技术网

Exit while true循环在没有更多可供单击的情况下使用Python和Selenium退出

Exit while true循环在没有更多可供单击的情况下使用Python和Selenium退出,python,selenium,web,while-loop,browser-automation,Python,Selenium,Web,While Loop,Browser Automation,我正在使用Python和Selenium自动化步骤/点击网站 我需要一个带有“while true:”循环的解决方案,以便在没有更多可点击的内容时退出并转到下一行代码 我一直在寻找,尽我所能尝试,但无法让这个工作 我访问并需要自动化的网页具有每天变化的可变数量的页面。 在每页的底部有一个“查看更多”按钮。 到目前为止,我的代码可以向下滚动并单击每个按钮,直到没有更多的按钮可单击为止 问题是,当没有更多可单击的内容时,代码会在出错时退出 如果有人能帮我解决问题,我将不胜感激 对于Python(或任

我正在使用Python和Selenium自动化步骤/点击网站

我需要一个带有“while true:”循环的解决方案,以便在没有更多可点击的内容时退出并转到下一行代码

我一直在寻找,尽我所能尝试,但无法让这个工作

我访问并需要自动化的网页具有每天变化的可变数量的页面。 在每页的底部有一个“查看更多”按钮。 到目前为止,我的代码可以向下滚动并单击每个按钮,直到没有更多的按钮可单击为止

问题是,当没有更多可单击的内容时,代码会在出错时退出

如果有人能帮我解决问题,我将不胜感激

对于Python(或任何编码),我是一个新手(但我正在尝试),所以请温柔地描述

多谢各位

            from selenium import webdriver
            from selenium.webdriver.common.keys import Keys
            import time
            url = "https://web_page_i_want_to_open.html"
            username = 'abcdefgh'
            password = '123456'
            browser  = webdriver.Firefox()
            browser.get(url)
            time.sleep(5)
            #find and click username field and insert login name
            browser.find_element_by_xpath('/path_to_login_name_field').send_keys(username)

            #find and click password field and insert password
            browser.find_element_by_xpath('/path_to_password_field').send_keys(password)

            #click login button
            browser.find_element_by_xpath('"login-button"]').click()

            #find and click "view more" button at bottom of page
            while True:
                browser.find_element_by_xpath('/button_at_bottom_of_the_page').click()
                time.sleep(5)

            #click "some_other_button_on_the_page"
            #browser.find_element_by_xpath('/some_other_button_on_the_page').click()

捕获无此类元素异常,当您得到该异常时,假设不再有页面

while True:
    try:
        browser.find_element_by_xpath('/button_at_bottom_of_the_page').click()
        time.sleep(5)
    except NoSuchElementException:
        break

怎么了?NoTouchElementException?抱歉,我没有指定错误。谢谢您的回复。考虑<<代码>尝试<代码/> >代码>除<代码> > <代码> > 循环。您还需要导入NoTouchElementException,因为它在defaultoops中不可用。是的,我有点写错了。