首先什么是句柄?句柄就是你點擊一個頁面,跳轉了一個新的窗口。你要操作的元素可能在原窗口上,也有可能在新窗口上。
看下圖句柄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()