gpt4 book ai didi

python - 当使用 selenium 编写测试时,如何查看生成的 HTML?

转载 作者:太空宇宙 更新时间:2023-11-03 18:56:47 24 4
gpt4 key购买 nike

以 python 中的以下 selenium 测试为例:

 import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

class PythonOrgSearch(unittest.TestCase):

def setUp(self):
self.driver = webdriver.Firefox()

def test_search_in_python_org(self):
driver = self.driver
driver.get("http://www.python.org")
self.assertIn("Python", driver.title)
elem = driver.find_element_by_name("q")
elem.send_keys("selenium")
elem.send_keys(Keys.RETURN)
self.assertIn("Google", driver.title)

def tearDown(self):
self.driver.close()

if __name__ == "__main__":
unittest.main()

取自:http://selenium-python.readthedocs.org/en/latest/getting-started.html#id2

结果输出类似于:

----------------------------------------------------------------------
Ran 1 test in 15.566s

OK

有什么方法可以让selenium在执行浏览器操作后输出html吗?

基本上,我使用适用于 Firefox 的 Selenium IDE 来记录浏览器上的操作。我想在 python 上播放它们,获取生成的 html,并根据该 html 采取进一步的操作(例如,第一个 selenium 测试可能是登录到网站并导航到某个地方。基于那里的内容,我想进行一个第二次测试(用户仍然登录))。使用 Selenium 可以吗?

提前致谢!

最佳答案

听起来好像您的测试最终可能会相互依赖,这是一个非常非常糟糕的想法。

尽管如此,page_source 函数将返回驱动程序正在查看的当前页面的完整 HTML:

https://code.google.com/p/selenium/source/browse/py/selenium/webdriver/remote/webdriver.py#429

关于python - 当使用 selenium 编写测试时,如何查看生成的 HTML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17103533/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com