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)
posted @ 2017-12-07 09:51  班然  阅读(302)  评论(0编辑  收藏  举报