使 selenium 等待直到打印网络请求中的所有 url

时间:2021-07-27 17:28:45

标签: python selenium selenium-webdriver

我使用 selenium-wirerequests 打印网络 URL、状态代码和一些标题属性。我希望 selenium 等到所有 url 都打印完再进入下一页。
如何添加等待相同的内容?
我的代码:

from seleniumwire import webdriver
import pytest
from selenium.webdriver.chrome.options import Options
import time


class Test_main():

    @pytest.fixture()
    def test_setup(self):
        # initiating browser
        chrome_options = Options()
        chrome_options.add_argument('--start-maximized')
        chrome_options.add_argument('--headless')

        self.driver = webdriver.Chrome(executable_path=r"D:/Python/Sel_python/drivers/chromedriver v86/chromedriver.exe", options=chrome_options)

        # terminate script
        yield
        self.driver.close()
        self.driver.quit()
        print("Test completed")

    
    def testcase_01(self, test_setup):
        self.driver.get("https://lifesciences.cactusglobal.com/")
        title = self.driver.title
        print("Page title: " + title)
        time.sleep(3)

        self.driver.find_element_by_xpath("//a[contains(text(),'here')]").click()
        self.driver.find_element_by_xpath("//a[contains(text(), 'Allow all')]").click()
        time.sleep(5)

        for request in self.driver.requests:
            if request.response:
                print(
                    request.url,
                    request.response.status_code,
                    request.response.headers['Content-Type'],
                    request.response.headers['set-cookie']
                )
        self.driver.find_element_by_xpath("//a[contains(text(),'next')]").click()

我尝试添加 time.sleep,但它会停止整个驱动程序,并在恢复时跳转到下一个 url。 有没有办法使用 webdriverwaits 处理这个问题?

0 个答案:

没有答案