zoukankan      html  css  js  c++  java
  • python3 程序问题解决列表

    环境:

    MacOS(10.13.1) + Python(3.6.1)

    python运行时提示WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

    brew install geckodriver
    

    注:在采集动态网页时,需要借助外部浏览器Firfox(版本:57.0.1)时,对DOM的操作需要通过geckodriver类库。在网上查找半天有的说是在Firfox某个版本之前是不需要geckodriver包,具体记不太清了。

    python3程序开发完成后,打包上传到服务器上执行遇到 No module named 'tornado'

    重新下载tornado,下载地址:https://pypi.python.org/pypi/tornado
    1、下载命令:wget https://pypi.python.org/packages/df/42/a180ee540e12e2ec1007ac82a42b09dd92e5461e09c98bf465e98646d187/tornado-4.5.1.tar.gz#md5=838687d20923360af5ab59f101e9e02e   
    2、解压:tar -zxvf tornado-4.5.1.tar.gz
    3、cd tornado-4.5.1 
    4、python setup.py build 
    5、python setup.py install 
    

    find_element_by_xpath 嵌套使用总是第一个元素解决方案

    lst_news = self.driver.find_elements_by_xpath('//ul[@class="sameday_list"]/li')
    for_i = 0
    for item in lst_news:
        # 第一种方案
        li_id = item.get_attribute('id')
        title = item.find_element_by_xpath('//li[@id="'+li_id+'"]/div/h2/span[@class="title"]').text
        print(title)
        # 第二种方案
        for_i += 1
        title2 = item.find_element_by_xpath('//li['+str(for_i)+']/div/h2/span[@class="title"]').text
        print(title2)
    

    问题原因:以下为内部实现方法,注意return是在父dom结构下去执行xpath。所以导致永远取到的是第一个值。所以我们在for中需要指定li标签的值

    # Private Methods
    def _execute(self, command, params=None):
        """Executes a command against the underlying HTML element.
    
        Args:
          command: The name of the command to _execute as a string.
          params: A dictionary of named parameters to send with the command.
    
        Returns:
          The command's JSON response loaded into a dictionary object.
        """
        if not params:
            params = {}
        params['id'] = self._id
        return self._parent.execute(command, params)
    
  • 相关阅读:
    secureCRT 6.5 ssh登陆openeuler提示交换秘钥不支持
    ediary电子日记本-力荐
    centos8.4 iso下载地址
    Manjaro Cutefish 安装体验类似macos风格
    脚本启动第一行提示sh/bash找不到
    securecrt双击克隆会话
    开源的window/linux远程连接工具-mRemoeteNG
    securecrt设置日志缓存
    怎么实现通过扫描二维码进行登录
    使用link rel="shortcut icon"为网页标题加图标
  • 原文地址:https://www.cnblogs.com/pengei/p/7997622.html
Copyright © 2011-2022 走看看