账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    selenium 无头模式获取日志performance出错
    53
    0

    网上看到的别人的代码,运行发现出错。
    无头模式下会报invalid argument: log type 'performance' not found错误,但去掉--headless即有头模式下不出错。

    请问如何解决?我需要无头下,可能查看performance日志

    python代码如下:

    from selenium import webdriver
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    import json
    
    
    def get_redurection_chain(url):
        """
        Given a url, return the urls in redirection chain and the length of the redirection chain.
        The redirection chain will be checked using selenium driven chrome browser and retrieved from
        browser log.
    
        :param url: the url that will be checked.
        :return: (
            length of redirection chain,
            a list of the urls in the redirection ordered based on the sequence they are visited,
        )
        """
        # landing_urls record origins->url->other intermedia urls->final_url
        landing_urls = list()
        landing_urls.append(url)
    
        curr_url = url
    
        capabilities = DesiredCapabilities.CHROME
        capabilities['loggingPrefs'] = {
            'performance': 'ALL',
        }
    
        options = webdriver.ChromeOptions()
        options.add_argument('--ignore-certificate-errors')
        options.add_argument('headless')
    
        driver = webdriver.Chrome(
            desired_capabilities=capabilities,
            chrome_options=options,
        )
    
        driver.get(url)
    
        for log in driver.get_log('performance'):
            log_entry = json.loads(log['message'])
    
            if 'redirectResponse' not in log_entry['message']['params']:
                continue
            if log_entry['message']['params']['redirectResponse']['url'] == curr_url:
                redirect_url = log_entry['message']['params']['request']['url']
                landing_urls.append(redirect_url)
                curr_url = redirect_url
    
        driver.close()
    
        return len(landing_urls), landing_urls
    
    if __name__ == '__main__':
        get_redurection_chain('http://www.baidu.com/')
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 善良的大宝 普通会员 1楼

      在Selenium中,无头模式下获取日志并出现性能问题通常涉及以下步骤:

      1. 配置Selenium webdriver:在项目中配置Selenium WebDriver。这通常涉及到安装Java WebDriver库,以及在代码中设置浏览器的open()方法和findElement()方法等方法的参数。以下是一个示例配置文件:

      ```java import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver;

      public class Main { public static void main(String[] args) { // 创建ChromeDriver实例 WebDriver driver = new ChromeDriver();

          // 启动浏览器并切换到无头模式
          driver.get("https://www.google.com");
      
          // 设置默认窗口大小
          driver.manage().window().maximize();
      
          // 设置浏览器选项,例如使用无头模式
          driver.manage().options().useHeadless();
      
          // 打开日志记录元素
          WebElement logElement = driver.findElement(By.id("log"));
      
          // 打开浏览器并关闭无头模式
          driver.quit();
      
          // 获取日志元素的文本内容
          String logContent = logElement.getText();
      
          // 在控制台打印日志内容
          System.out.println(logContent);
      }
      

      } ```

      1. 检查性能问题:确保无头模式下,日志记录元素确实存在且在页面中,且能正常打印出日志内容。如果上述步骤正常,那么性能问题可能是由以下几种原因导致的:

      2. WebDriver的初始化问题:可能是因为 WebDriver 的初始化步骤未正确完成,导致浏览器未进入无头模式。检查 ChromeDriver.get() 方法的返回值,确保它返回的是一个有效的 WebDriver 对象。

      3. 无头模式设置问题:在上述示例中,我们使用了 useHeadless() 方法打开了无头模式,但可能没有正确设置 selenium.options() 配置项来关闭无头模式。确保你已经正确关闭了无头模式,并且设置了 selenium.options() 选项的 headless 属性为 false
      4. 性能优化:优化页面加载速度、减少网络请求、提高代码的执行效率等都可以改善无头模式下日志记录的性能。例如,可以使用 Seleniumium.javaProcessExecutor 实现异步加载页面、使用 Seleniumium.locateElement() 方法找到日志元素、使用 Seleniumium.executeJavaScript() 方法执行JavaScript代码等。
      5. 调试工具:使用 Chrome DevTools 的性能工具(如 TimelineConsoleNetwork 等)进行性能调试,查看各个步骤的执行时间,找出性能瓶颈。

      如果上述步骤都无法解决问题,可能需要进一步检查页面结构、浏览器的配置、JavaScript代码等,以确定具体问题所在并采取相应措施。在实际项目中,可能还需要与开发团队和产品经理进行沟通,共同找到解决日志性能问题的最佳方式。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部