关于selenium自动化对窗口句柄的处理

首先什么是句柄?句柄就是你点击一个页面,跳转了一个新的窗口。你要操作的元素可能在原窗口上,也有可能在新窗口上。

看下图句柄1

句柄2

由这2张图可知,url不一样,证明他们是处于不同的界面,我要操作的元素是在句柄2上。

处理方式,先用print(self.driver.window_handles)打印出2个界面的句柄,一个打印跳转界面前的,一个打印跳转之后。打印出来如下图,是个列表

然后看你要操作的元素在哪个界面上,比如下标为0的句柄对应的是跳转之前的界面,下标为1的就代表跳转之后的句柄。

用 self.driver.switch_to.window(self.driver.window_handles[1])选择你要跳转的句柄,我这里要操作的界面是跳转之后所以选择的1。跳转之后就可以继续定位元素了。这里的self是类本身,如果不是用面向对象的方式写的可以不加这个。

窗口句柄的第二种解决方式,简单说下,可以用close关闭当前窗口,这里关闭,默认关的是跳转之前的,不是跳转之后的。这样句柄就只剩下一个了。然后在用switch_to选择下标为0的句柄。就可以就行操作了。只适用于要操作的元素是在跳转之后的界面。


上代码:
from selenium import webdriver
import time
import multiprocessing


class Zutuan():
    def __init__(self):
        """打开浏览器"""
        self.driver = webdriver.Chrome()

    def open_zutuan(self, url):
        """传入组团url"""
        self.driver.get(url)
        self.driver.maximize_window()
        self.driver.refresh()
        #time.sleep(0.01)
        self.driver.implicitly_wait(30)       # todo implicitly隐式等待,等待元素可见

    def option_element(self, user, password):
        """手写xpath定位元素"""
        self.driver.find_element_by_xpath('//div[@class="login a"]/i').click()
        time.sleep(0.01)
        self.driver.find_element_by_xpath('//div[@class="a-title"]').click()
        self.driver.find_element_by_xpath('//input[@type="text" or @class="userName"]').send_keys(user)
        self.driver.find_element_by_xpath('//input[@type="password"]').send_keys(password)
        self.driver.find_element_by_xpath('//div[@class="button"]').click()
        time.sleep(0.1)

    def select_commodity(self, content):
        """搜索组团商品"""
        # TODO self.content实例属性传给下面的方法使用,如果想把值给下面的方法用,添加实例属性解决
        self.content = content
        self.driver.find_element_by_xpath('//input[@type="text"]').send_keys(content)
        self.driver.find_element_by_xpath('//div[@class="search"]').click()
        return content

    def result(self):
        """判断搜索商品成功后的提示信息,断言页面是否成功"""
        if self.content in self.driver.page_source:
            #print(self.content)
            print('商品搜索成功,测试通过')
        else:
            print('商品搜索错误,测试失败')

    def closed(self):
        """关闭浏览器"""
        time.sleep(1)
        self.driver.quit()


def run1():
    # TODO 根据操作顺序,调用方法执行
    zt = Zutuan()
    zt.open_zutuan('http://www.zutuan.cn/index.html#/')
    zt.option_element('1489@qq.com', 'mg123456')
    zt.select_commodity('木瓜')
    zt.result()
    zt.closed()


class View_details(Zutuan):
    """把商品添加为明星单品,"""
    def check_commodity(self, number):
        """进入商品详情页,点击添加明星单品"""
        self.driver.find_element_by_xpath('//a[@target="_blank"]/img').click()
        self.driver.switch_to.window(self.driver.window_handles[1])
        self.driver.find_element_by_xpath('//div[@class="child start"]').click()
        self.driver.find_element_by_xpath('//div[@class="el-dialog__body"]//input[@type="text"]').send_keys(number)
        self.driver.find_element_by_xpath('//button[@type="button" and @class="el-button el-button--danger"]').click()
        time.sleep(0.1)

    def result(self):
        """重写父类方法,判断商品添加成功后的提示信息,断言页面是否成功"""
        if '添加成功' in self.driver.page_source:
            print('商品添加成功,测试通过')
        else:
            print('商品添加失败,测试失败')
        # 调用父类方法关闭
        super().closed()


def run2():
    vd = View_details()
    vd.open_zutuan('http://www.zutuan.cn/index.html#/')
    vd.option_element('14861@qq.com', 'mg123456')
    vd.select_commodity('裤子')
    vd.check_commodity(9168)
    vd.result()


def main():
    p1 = multiprocessing.Process(target=run1)
    p2 = multiprocessing.Process(target=run2)

    p1.start()
    p2.start()


if __name__ == '__main__':
    main()
原文地址:https://www.cnblogs.com/xiamaojjie/p/11385399.html