from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
import time
web = Chrome()
web.get("http://lagou.com")
el = web.find_element_by_xpath('//*[@id="changeCityBox"]/p[1]/a')
el.click()
web.find_element_by_xpath('//*[@id="search_input"]').send_keys("python", Keys.ENTER)
time.sleep(1)
web.find_elements_by_xpath('//*[@id="openWinPostion"]')[0].click()
"""
产生新窗口, 如何切换新窗口
web.switch_to.window()
web.window_handles[-1]:-1表示最后一个窗口
"""
web.switch_to.window(web.window_handles[-1])
# 在新窗口进行操作
detail_text = web.find_element_by_xpath('//*[@id="job_detail"]/dd[2]/div').text
print(detail_text)
# 关闭新窗口
web.close()
# 视角变更(web)
web.switch_to.window(web.window_handles[0])
print(web.find_elements_by_xpath('//*[@id="openWinPostion"]')[0].text)
"""
如果页面遇见了iframe窗口,就出现问题
解决:
1.定位到iframe
2.切换视角到iframe
web.switch_to.frame(iframe)
3.切换出来
web.switch_to.default_content()
"""
selenium_窗口切换
最新推荐文章于 2024-04-30 10:44:01 发布