Selenium頁(yè)面跳轉(zhuǎn)后的元素定位-switch_to.window()
背景:
在使用Selenium獲取網(wǎng)頁(yè)數(shù)據(jù)時(shí)常常會(huì)因?yàn)轫?yè)面跳轉(zhuǎn)導(dǎo)致,后續(xù)的頁(yè)面數(shù)據(jù)無(wú)法正常獲取。究極原因,都是因?yàn)榇藭r(shí)的頁(yè)面對(duì)象還是上一個(gè)頁(yè)面,使用當(dāng)前頁(yè)面的定位條件當(dāng)然會(huì)報(bào)錯(cuò)(因?yàn)樯弦粋€(gè)頁(yè)面根本不存在這些定位條件)。因此我們可以通過(guò)switch_to.window()進(jìn)行頁(yè)面的切換。
使用方法
switch_to.window()存在以下幾種用法,下面是詳細(xì)示例講解:
URL:http://www.gjgwy.net/zkzx/gjgwy/345695.html(公考資料網(wǎng))
driver.switch_to.window(driver.window_handles[-1]) 定位到最新打開(kāi)窗口
driver.switch_to.window(driver.window_handles[-2]) 定位到倒數(shù)第二個(gè)窗口
driver.switch_to.window(driver.window_handles[0]) 定位最開(kāi)始的頁(yè)面
1、driver.switch_to.window(driver.window_handles[-1]) 定位到最新打開(kāi)窗口
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
url='http://www.gjgwy.net/zkzx/gjgwy/345695.html'
# 初始實(shí)例化-谷歌瀏覽器
driver=webdriver.Chrome()
#獲取網(wǎng)址信息
driver.get(url)
#判斷是否有彈窗,存在彈窗則關(guān)閉,沒(méi)有繼續(xù)執(zhí)行即可
try:
driver.find_element(By.XPATH, '//*[@id="close_alert"]').click()
except:
print("沒(méi)有彈窗,繼續(xù)執(zhí)行")
# 點(diǎn)擊浙江省份,查看浙江省的公考信息,實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn),打開(kāi)新的頁(yè)面。
driver.find_element(By.XPATH, '/html/body/div[4]/div/div[2]/a[2]').click()
time.sleep(2)
# 定位到新開(kāi)頁(yè)面
driver.switch_to.window(driver.window_handles[-1])
time.sleep(2)
# 判斷是否存在彈窗,存在就獲取彈窗,然后關(guān)閉;
#若網(wǎng)頁(yè)刷新速度很慢,彈窗未出,就已經(jīng)拿到元素信息,就不需要關(guān)閉彈窗;
try:
driver.find_element(By.XPATH, '//*[@id="close_alert"]').click()
except:
print("沒(méi)有彈窗,繼續(xù)執(zhí)行")
next_page =driver.find_element(By.XPATH,'/html/body/table[1]/tbody/tr[1]/td[2]/div/div/ul/li[2]')
print(next_page.text)
time.sleep(1000)
# 關(guān)閉頁(yè)面
driver.close()
此時(shí)next_page獲取的值即為打開(kāi)的第二個(gè)頁(yè)面上的元素值,而非第一個(gè)頁(yè)面的。
*next_page=【職位表】2022年遼寧公務(wù)員考試職位表*
倘若把切換窗口的代碼注釋掉,則會(huì)上報(bào)錯(cuò)誤提示不存在的定位信息,如下:
# driver.switch_to.window(driver.window_handles[-1])
D:\workplace\venv\Scripts\python.exe D:\workplace\venv\Scripts\tietie.py
Traceback (most recent call last):
File "D:\workplace\venv\Scripts\tietie.py", line 19, in <module>
next_page=driver.find_element(By.XPATH,'/html/body/table[1]/tbody/tr[1]/td[2]/div/div/ul/li[2]')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\workplace\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 861, in find_element
return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\workplace\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 444, in execute
self.error_handler.check_response(response)
File "D:\workplace\venv\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 249, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/table[1]/tbody/tr[1]/td[2]/div/div/ul/li[2]"}
(Session info: chrome=105.0.5195.102)
Stacktrace:
Backtrace:
Ordinal0 [0x0029DF13+2219795]
Ordinal0 [0x00232841+1779777]
Ordinal0 [0x0014423D+803389]
Ordinal0 [0x00173025+995365]
Ordinal0 [0x001731EB+995819]
Ordinal0 [0x001A0F52+1183570]
Ordinal0 [0x0018E844+1108036]
Ordinal0 [0x0019F192+1175954]
Ordinal0 [0x0018E616+1107478]
Ordinal0 [0x00167F89+950153]
Ordinal0 [0x00168F56+954198]
GetHandleVerifier [0x00592CB2+3040210]
GetHandleVerifier [0x00582BB4+2974420]
GetHandleVerifier [0x00336A0A+565546]
GetHandleVerifier [0x00335680+560544]
Ordinal0 [0x00239A5C+1808988]
Ordinal0 [0x0023E3A8+1827752]
Ordinal0 [0x0023E495+1827989]
Ordinal0 [0x002480A4+1867940]
BaseThreadInitThunk [0x7728FEF9+25]
RtlGetAppContainerNamedObjectPath [0x77447BBE+286]
RtlGetAppContainerNamedObjectPath [0x77447B8E+238]
(No symbol) [0x00000000]
2、driver.switch_to.window(driver.window_handles[-2]) 定位到倒數(shù)第二個(gè)窗口
此時(shí)我們?cè)谏厦娴幕A(chǔ)上,在跳轉(zhuǎn)一個(gè)頁(yè)面,來(lái)實(shí)現(xiàn)打開(kāi)三個(gè)窗口的情況。
當(dāng)然此時(shí),不用driver.switch_to.window(driver.window_handles[-1]),這條指令,此時(shí)的頁(yè)面指向也是第二個(gè)頁(yè)面,如下,但是為了展示[-2]的作用,在這里先把頁(yè)面定位到最新的窗口,然后再通過(guò)命令定位到倒數(shù)第二個(gè)頁(yè)面。
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
url='http://www.gjgwy.net/zkzx/gjgwy/345695.html'
# 初始實(shí)例化-谷歌瀏覽器
driver=webdriver.Chrome()
#獲取網(wǎng)址信息
driver.get(url)
try:
driver.find_element(By.XPATH, '//*[@id="close_alert"]').click()
except:
print("沒(méi)有彈窗,繼續(xù)執(zhí)行")
# 點(diǎn)擊浙江省份,查看浙江省的公考信息,實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn),打開(kāi)新的頁(yè)面。
driver.find_element(By.XPATH, '/html/body/div[4]/div/div[2]/a[2]').click()
# 定位到新開(kāi)頁(yè)面
driver.switch_to.window(driver.window_handles[-1])
time.sleep(5)
# 判斷是否存在彈窗,存在就獲取彈窗,然后關(guān)閉;
#若網(wǎng)頁(yè)刷新速度很慢,彈窗未出,就已經(jīng)拿到元素信息,就不需要關(guān)閉彈窗;
try:
driver.find_element(By.XPATH, '//*[@id="close_alert"]').click()
except:
print("沒(méi)有彈窗,繼續(xù)執(zhí)行")
time.sleep(2)
# 跳轉(zhuǎn)到第三個(gè)頁(yè)面
driver.find_element(By.XPATH, '/html/body/table[1]/tbody/tr[1]/td[2]/div/div/ul/li[2]').click()
# 定位到最新的第三個(gè)頁(yè)面上
driver.switch_to.window(driver.window_handles[-1])
# 輸出最新頁(yè)面上的信息
e=driver.find_element(By.XPATH,'/html/body/div[10]/div[1]/div[1]/div[2]')
print(e.text)
# 通過(guò)命令定位到倒數(shù)第二頁(yè)面
driver.switch_to.window(driver.window_handles[-2])
# 輸出倒數(shù)第二個(gè)頁(yè)面的信息
a=driver.find_element(By.XPATH,'/html/body/table[1]/tbody/tr[1]/td[2]/div/div/ul/li[2]')
print(a.text)
time.sleep(1000)
# 關(guān)閉頁(yè)面
driver.close()
輸出結(jié)果為:
#第三個(gè)頁(yè)面的信息
2022年遼寧省考試錄用公務(wù)員公告及職位表現(xiàn)已發(fā)布,報(bào)名時(shí)間為2022年2月18日9時(shí)至2月22日24時(shí),筆試時(shí)間為2022年3月26日、27日。
點(diǎn)擊下載:2022年遼寧省考試錄用公務(wù)員職位表(提取碼:2h97)
2022年遼寧公務(wù)員考試職位表
上一篇: 2022年河南公務(wù)員考試網(wǎng)站及政策咨詢電話
下一篇: 2022年遼寧公務(wù)員考試報(bào)考指南
#倒數(shù)第二個(gè)頁(yè)面的信息
【職位表】2022年遼寧公務(wù)員考試職位表
3、driver.switch_to.window(driver.window_handles[0]) 定位最開(kāi)始的頁(yè)面
還是沿用上面的,三個(gè)頁(yè)面狀態(tài),直接定位到第一個(gè)頁(yè)面上的信息。
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
url='http://www.gjgwy.net/zkzx/gjgwy/345695.html'
# 初始實(shí)例化-谷歌瀏覽器
driver=webdriver.Chrome()
#獲取網(wǎng)址信息
driver.get(url)
#存在彈窗即可關(guān)閉,不存在繼續(xù)執(zhí)行
try:
driver.find_element(By.XPATH, '//*[@id="close_alert"]').click()
except:
print("沒(méi)有彈窗,繼續(xù)執(zhí)行")
# 點(diǎn)擊浙江省份,查看浙江省的公考信息,實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn),打開(kāi)新的頁(yè)面。
driver.find_element(By.XPATH,'/html/body/div[4]/div/div[2]/a[2]').click()
time.sleep(2)
# 定位到新開(kāi)頁(yè)面
driver.switch_to.window(driver.window_handles[-1])
time.sleep(5)
# 判斷是否存在彈窗,存在就獲取彈窗,然后關(guān)閉;
#若網(wǎng)頁(yè)刷新速度很慢,彈窗未出,就已經(jīng)拿到元素信息,就不需要關(guān)閉彈窗;
try:
driver.find_element(By.XPATH, '//*[@id="close_alert"]').click()
except:
print("沒(méi)有彈窗,繼續(xù)執(zhí)行")
# 跳轉(zhuǎn)到第三個(gè)頁(yè)面
driver.find_element(By.XPATH,'/html/body/table[1]/tbody/tr[1]/td[2]/div/div/ul/li[2]').click()
# 定位到最新的第三個(gè)頁(yè)面上
driver.switch_to.window(driver.window_handles[-1])
# 輸出最新頁(yè)面上的信息
e=driver.find_element(By.XPATH,'/html/body/div[10]/div[1]/div[1]/div[2]')
print(e.text)
# 通過(guò)命令定位到最開(kāi)始的窗口
driver.switch_to.window(driver.window_handles[0])
# 若存在彈窗,則關(guān)掉
try:
driver.find_element(By.XPATH, '//*[@id="close_alert"]').click()
except:
print("沒(méi)有彈窗,繼續(xù)執(zhí)行")
# 輸出最開(kāi)始窗口的信息
a=driver.find_element(By.XPATH,'/html/body/div[4]/div/div[2]/a[2]')
print(a.text)
time.sleep(1000)
# 關(guān)閉頁(yè)面
driver.close()
輸出結(jié)果為:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-405884.html
D:\workplace\venv\Scripts\python.exe D:\workplace\venv\Scripts\tietie.py
#最后頁(yè)面的信息
2022年遼寧省考試錄用公務(wù)員公告及職位表現(xiàn)已發(fā)布,報(bào)名時(shí)間為2022年2月18日9時(shí)至2月22日24時(shí),筆試時(shí)間為2022年3月26日、27日。
點(diǎn)擊下載:2022年遼寧省考試錄用公務(wù)員職位表(提取碼:2h97)
2022年遼寧公務(wù)員考試職位表
上一篇: 2022年河南公務(wù)員考試網(wǎng)站及政策咨詢電話
下一篇: 2022年遼寧公務(wù)員考試報(bào)考指南
#最開(kāi)始頁(yè)面信息
遼寧
總結(jié)
在執(zhí)行測(cè)試過(guò)程中會(huì)存在兩個(gè)細(xì)節(jié)但是影響進(jìn)程的問(wèn)題:
1、進(jìn)入一個(gè)新頁(yè)面,有可能存在彈窗的情況,如果存在彈窗,那么當(dāng)前頁(yè)面就會(huì)定位在彈窗上,那一定不是你想要的元素,因此,我們可以關(guān)閉它,之后再繼續(xù)執(zhí)行。
2、執(zhí)行過(guò)程中發(fā)現(xiàn),有的時(shí)候,代碼,元素條件都沒(méi)有問(wèn)題,但是就是報(bào)錯(cuò),這是有可能網(wǎng)速的問(wèn)題,我們要學(xué)會(huì)善用time.sleep這個(gè)語(yǔ)句,預(yù)留充足的時(shí)間可以繼續(xù)執(zhí)行。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-405884.html
到了這里,關(guān)于Selenium頁(yè)面跳轉(zhuǎn)后的元素定位-switch_to.window()使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!