Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.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
Python 如何在每次不重新启动浏览器的情况下测试Selenium自动化?_Python_Selenium_Automation - Fatal编程技术网

Python 如何在每次不重新启动浏览器的情况下测试Selenium自动化?

Python 如何在每次不重新启动浏览器的情况下测试Selenium自动化?,python,selenium,automation,Python,Selenium,Automation,假设我有以下代码: from selenium import webdriver if __name__ == '__main__': def open_YouTube(): browser = webdriver.Firefox() browser.get("http://www.google.pl") fill_search(browser) def fill_search(browser): elemen

假设我有以下代码:

from selenium import webdriver

if __name__ == '__main__':

    def open_YouTube():
        browser = webdriver.Firefox()
        browser.get("http://www.google.pl")
        fill_search(browser)

    def fill_search(browser):
        elements = browser.find_elements_by_tag_name('input')
        for element in elements:
            if 'what are you looking for' in element.get_attribute('innerHTML'):
                search_box = element
                search_box.get_attribute('value') == 'happiness'



    open_YouTube()
现在,假设我犯了一个错误,或者想更改函数
fill\u search
中的任何内容,有没有一种方法可以只测试该函数,而不必从一开始就运行代码并重新打开浏览器

例如,在我的Internet Explorer automations via VBA中,我有一个按名称或URL标识当前打开的浏览器实例的函数。我可以运行这个函数,只测试我想要更正的代码,在已经打开的浏览器实例上工作

硒也有类似的可能吗?

有简单(推荐)的方法,也有复杂的方法

简单:

只需导入pdb并在浏览器会话中尝试功能。一旦一切正常运行,请更新代码并删除pdb行

def fill_search():
        import pdb; pdb.set_trace()  # Experiment in the browser session
        for element in elements:
            element = driver.find_element_by_tag('input')
            if 'what are you looking for' in element.get_attribute('innerHTML'):
                search_box = element
                search_box.get_attribute('value') == 'happiness'
复杂:

driver = webdriver.Firefox()
session_id = driver.session_id
executor_url = driver.command_executor._url
same_session_driver = webdriver.Remote(command_executor=executor_url, desired_capabilities={})
same_session_driver.session_id = session_id
same_session_driver.continue_doing_stuff

如果我想在调试期间更改下面的代码
set\u trace
?调试器似乎“记住”了旧代码。您可能误解了Python解释器的工作原理。运行脚本后,.py文件的文本将被读取和解释-从那时起,所有内容都会在内存中发生,并且在再次运行脚本之前,.py文件不再相关,因此您必须更改代码,保存它,然后重新运行以查看更改。