python selenium涓璱frame鍒囨崲銆亀indow鍒囨崲鏂规硶
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python selenium涓璱frame鍒囨崲銆亀indow鍒囨崲鏂规硶相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%5b1%5d' title='[1]'>[1]
text 鎵撳紑 one str size ble nbsp windows涓€銆乻elenium涓璱frame鍒囨崲鏂规硶锛?/h2>
銆€銆€銆€銆€鏂规硶涓€锛歴witch_to.frame
銆€銆€銆€銆€銆€銆€frame鍑芥暟涓彁渚涗簡涓夌瀹氫綅鏂规硶锛?/span>
-
- 銆€銆€driver.switch_to.frame(鈥榝rame_name鈥?
- 銆€銆€driver.switch_to.frame(1)
- 銆€銆€driver.switch_to.frame(driver.find_elements_by_tag_name("iframe")[0])
銆€銆€浠ヨ吘璁鍫備负渚嬶細
from selenium import webdriver from time import sleep driver = webdriver.Chrome() driver.get(鈥?/span>https://ke.qq.com/鈥?/span>) driver.implicitly_wait(20) driver.maximize_window() # 鐐瑰嚮鐧诲綍 driver.find_element_by_id(鈥?/span>js_login鈥?/span>).click() # 鐐瑰嚮QQ鐧婚檰 driver.find_element_by_xpath(鈥?/span>//div[@class="content-btns"]/a[1]鈥?/span>).click() # 鍒囨崲iframe driver.switch_to.frame(鈥?/span>login_frame_qq鈥?/span>) # 鐐瑰嚮璐﹀彿瀵嗙爜鐧婚檰 driver.find_element_by_id(鈥?/span>switcher_plogin鈥?/span>).click() # 杈撳叆甯愬彿 driver.find_element_by_id(鈥?/span>u鈥?/span>).send_keys(鈥?/span>*********鈥?/span>) # 杈撳叆瀵嗙爜 driver.find_element_by_id(鈥?/span>p鈥?/span>).send_keys(鈥?/span>*********鈥?/span>) # 鐐瑰嚮鐧诲綍 driver.find_element_by_id(鈥?/span>login_button鈥?/span>).click() sleep(3) driver.quit()
銆€銆€銆€銆€鏂规硶浜岋細expected_conditions涓璮rame_to_be_available_and_switch_to_it
銆€銆€銆€銆€銆€銆€瀹炵幇姝ラ锛氱粨鍚堟樉鎬х瓑寰呮柟娉曪紝绛夊緟iframe妗嗗嚭鐜板悗鑷姩鍒囨崲iframe锛屽叾鏂规硶鍐呴儴璋冪敤浜?switch_to.frame 鏂规硶锛堝彲浠ョ湅婧愮爜锛夛紝鎵€浠ュ畾浣峣frame鐨勬柟娉曚笌鏂规硶涓€鐩稿悓銆?/span>
銆€銆€浠ヨ吘璁鍫備负渚嬶細
from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from time import sleep driver = webdriver.Chrome() driver.get(鈥?/span>https://ke.qq.com/鈥?/span>) driver.implicitly_wait(20) driver.maximize_window() # 鐐瑰嚮鐧诲綍 driver.find_element_by_id(鈥?/span>js_login鈥?/span>).click() # 鐐瑰嚮QQ鐧婚檰 driver.find_element_by_xpath(鈥?/span>//div[@class="content-btns"]/a[1]鈥?/span>).click() # 绛夊緟iframe鍑虹幇,鑷姩鍒囨崲iframe WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it("login_frame_qq")) # 鐐瑰嚮璐﹀彿瀵嗙爜鐧婚檰 driver.find_element_by_id(鈥?/span>switcher_plogin鈥?/span>).click() # 杈撳叆甯愬彿 driver.find_element_by_id(鈥?/span>u鈥?/span>).send_keys(鈥?/span>*********鈥?/span>) # 杈撳叆瀵嗙爜 driver.find_element_by_id(鈥?/span>p鈥?/span>).send_keys(鈥?/span>*********鈥?/span>) # 鐐瑰嚮鐧诲綍 driver.find_element_by_id(鈥?/span>login_button鈥?/span>).click() sleep(3) driver.quit()
浜屻€乻elenium涓瓀indow鍒囨崲鏂规硶锛?/h2>
銆€銆€銆€銆€鏂规硶涓€锛歴witch_to.window
銆€銆€鐧惧害鎼滅储鑵捐璇惧爞骞舵墦寮€銆€銆€
from selenium import webdriver from time import sleep # 璁剧疆娴忚鍣ㄩ厤缃紝鍙栨秷‘Chrome姝e湪鍙楀埌鑷姩杞欢鐨勬帶鍒?rsquo;鐨勬彁绀鸿 option = webdriver.ChromeOptions() option.add_argument(鈥?/span>disable-infobars鈥?/span>) # 鎵撳紑chrome娴忚鍣?骞舵墽琛岃缃?/span> driver = webdriver.Chrome(chrome_options=option) driver.implicitly_wait(30) driver.maximize_window() # 鎵撳紑鐧惧害棣栭〉 driver.get("https://www.baidu.com") # 杈撳叆 鑵捐璇惧爞 driver.find_element_by_id(鈥?/span>kw鈥?/span>).send_keys(鈥?/span>鑵捐璇惧爞鈥?/span>) # 鐐瑰嚮 鐧惧害涓€涓?/span> driver.find_element_by_id("su").click() # 鑾峰彇鎵€鏈夌獥鍙?/span> windows = driver.window_handles print(鈥?/span>鍒囨崲涔嬪墠鐨勭獥鍙}鈥?/span>.format(windows)) # 鐐瑰嚮鑵捐璇惧爞閾炬帴 driver.find_elements_by_xpath(鈥?/span>//a[text()="_涓撲笟鐨勫湪绾挎暀鑲插钩鍙?ke.qq.com)"]鈥?/span>)[0].click() # 鍒囨崲鍒版柊绐楀彛 driver.switch_to.window(driver.window_handles[-1]) print(鈥?/span>鍒囨崲鑷冲悗鐨勭獥鍙}鈥?/span>.format(driver.current_window_handle)) # 鐐瑰嚮鐧诲綍鎸夐挳 driver.find_element_by_id(鈥?/span>js_login鈥?/span>).click()
銆€銆€銆€銆€鏂规硶浜岋細expected_conditions涓璶ew_window_is_opened
銆€銆€銆€銆€銆€銆€瀹炵幇姝ラ锛氱粨鍚堟樉鎬х瓑寰呮柟娉曪紝绛夊緟鏂扮獥鍙f墦寮€锛屽啀杩涜鍒囨崲
銆€銆€鐧惧害鎼滅储鑵捐璇惧爞骞舵墦寮€
from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from time import sleep # 璁剧疆娴忚鍣ㄩ厤缃紝鍙栨秷‘Chrome姝e湪鍙楀埌鑷姩杞欢鐨勬帶鍒?rsquo;鐨勬彁绀鸿 option = webdriver.ChromeOptions() option.add_argument(鈥?/span>disable-infobars鈥?/span>) # 鎵撳紑chrome娴忚鍣?骞舵墽琛岃缃?/span> driver = webdriver.Chrome(chrome_options=option) driver.implicitly_wait(30) driver.maximize_window() # 鎵撳紑鐧惧害棣栭〉 driver.get("https://www.baidu.com") # 杈撳叆 鑵捐璇惧爞 driver.find_element_by_id(鈥?/span>kw鈥?/span>).send_keys(鈥?/span>鑵捐璇惧爞鈥?/span>) # 鐐瑰嚮 鐧惧害涓€涓?/span> driver.find_element_by_id("su").click() # 鑾峰彇鎵€鏈夌獥鍙?/span> windows = driver.window_handles print(鈥?/span>鍒囨崲涔嬪墠鐨勭獥鍙}鈥?/span>.format(windows)) # 鐐瑰嚮鑵捐璇惧爞閾炬帴 driver.find_elements_by_xpath(鈥?/span>//a[text()="_涓撲笟鐨勫湪绾挎暀鑲插钩鍙?ke.qq.com)"]鈥?/span>)[0].click() # 鍒ゆ柇鏂扮獥鍙f墦寮€ WebDriverWait(driver,10).until(EC.new_window_is_opened(windows)) # 鍒囨崲鍒版柊绐楀彛 driver.switch_to.window(driver.window_handles[-1]) print(鈥?/span>鍒囨崲鑷冲悗鐨勭獥鍙}鈥?/span>.format(driver.current_window_handle)) # 鐐瑰嚮鐧诲綍鎸夐挳 driver.find_element_by_id(鈥?/span>js_login鈥?/span>).click()
以上是关于python selenium涓璱frame鍒囨崲銆亀indow鍒囨崲鏂规硶的主要内容,如果未能解决你的问题,请参考以下文章