• day2_窗口句柄切换


    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Time    : 2019/7/16 14:21
    # @Author  : 大坏男孩
    # @File    : day2_窗口句柄切换.py
    # @Software: PyCharm
    # @license : Copyright(C), 未来集团有限公司
    # @Contact : 110@163.com
    
    import time
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    
    # driver = webdriver
    driver = webdriver.Chrome()
    driver.get("http://www.baidu.com")
    driver.maximize_window()
    time.sleep(2)
    driver.find_element_by_id("kw").send_keys("鸡你太美是什么梗")
    
    # driver.window_handles  -- 获取浏览器所有窗口的句柄,返回一个列表
    window_a = driver.window_handles
    # 打印窗口window_a的句柄
    print(window_a)
    # 在百度输入框后面添加一个value值为ENTER的内容
    driver.find_element_by_id("kw").send_keys(Keys.ENTER)
    time.sleep(2)
    # 搜索界面第一条链接
    driver.find_element_by_xpath('//*[@id="1"]/h3/a').click()
    window_b = driver.window_handles
    # 打印窗口window_b的句柄
    print(window_b)
    
    # 切换窗口句柄
    driver.switch_to.window(window_b[1])
    time.sleep(2)
    driver.find_element_by_xpath('//span[@id="evaluate-3047402417"]').click()
    time.sleep(2)
    # # 随意打开栏目等你来答下的一个内容  //*[@id="wgt-ad-right-fixed"]
    # driver.find_element_by_xpath('//*[@id="wgt-ad-right-fixed"]/div[1]/ul/li[2]/div/a').click()
    # # 切换为原窗口,即返回上一个网页界面
    # driver.switch_to.window(window_b[0])
    # time.sleep(2)
    # driver.find_element_by_xpath('//*[@id="2"]/h3/a').click()
    # 点击更多回答
    driver.find_element_by_id("show-answer-hide").click()
    time.sleep(2)
    # # driver.find_elements_by_xpath('//span[@class="evaluate evaluate-32  evaluate-good"]')
    # # ls = driver.find_element_by_xpath('//span[@class="evaluate evaluate-32  evaluate-good"]')
    # # print(ls)
    # # print(type(ls))
    ls = driver.find_elements_by_xpath('//b[@class="evaluate-num"]')
    print(ls)      # 返回列表
    print(type(ls))     # 列表
    print(len(ls))   # 打印列表ls的长度
    # for循环   -->实现循环点赞
    n = 1
    for i in ls:
        if n % 2 != 0:
            # print(i)
            i.click()
        n += 1
    # 返回第二页的元素坐标位置
    driver.find_element_by_xpath('//*[@id="wgt-answers"]/div/div[5]/a[1]').click()
    ls = driver.find_elements_by_xpath('//b[@class="evaluate-num"]')
    n = 2
    for i in ls:
        if n % 2 == 0:
            i.click()
        n += 1
    

      

    一行代码一行诗
  • 相关阅读:
    Lambda
    Guava
    创建数据库时报错 'str' object has no attribute 'decode'
    服务器并发测试(jmeter)
    Mosquitto 创建用户自动输入密码
    menuconfig 语法与用法
    Django操作mongodb
    mqtt mosquitto 安装与使用
    python 使用mongodb数据库
    djangorestframework token 认证
  • 原文地址:https://www.cnblogs.com/huainanhai/p/11197084.html
Copyright © 2020-2023  润新知