selenium_简单操作

from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys

web = Chrome()

# web.get(‘http://www.lagou.com‘)
#
# # 浏览器窗口最大化
# web.maximize_window()
#
# #找到某个元素,点击他
# el = web.find_element_by_xpath(‘....‘)
#
# el.click()
#
# #找到输入框,输入python -->>> 输入回车,点击搜索按钮
# web.find_element_by_xpath(‘....‘).send_keys(‘python‘, Keys.ENTER)
#
# #查找存放数据的位置,进行数据提取
# #找到页面中所有的li标签
# lis = web.find_elements_by_xpath(‘..../li‘)
# for li in lis:
#     jobName = li.find_element_by_tag_name(‘h3‘).text
#     jobPrice = li.find_element_by_xpath(‘./..../span‘).text
#     companyName = li.find_element_by_xpath(‘./..../a‘).text
#     print(jobName, jobPrice, companyName)
#
#
# #如何进入到新窗口中提取数据
# #注意:在selenium眼中,新窗口默认是不切换的
# web.switch_to.window(web.window_handles[-1])   # window_handles[-1] 就是最后一个窗口
#
# #在新窗口中提取数据
# jobDetail = web.find_element_by_xpath(‘//..../div‘).text
# print(jobPrice)
#
# #关闭子窗口
# web.close()
# #变更selenium窗口视角,回到原来的窗口
# web.switch_to.window(web.window_handles[0])


# 如果页面中遇到了 iframe 标签
web.get(http://www.91kanju.com/vodd-play/541-2-1.html)

#处理 iframe 的话,必须先拿到 iframe,然后切换视角到 iframe,之后才可以拿到数据
iframe = web.find_element_by_xpath(//*[@id="player_iframe"])
web.switch_to.frame(iframe)  # 切换到 iframe
txt = web.find_element_by_xpath(//*[@id="main"]/h3[1]).text  # 获取iframe中的部分数据
print(txt)
web.switch_to.default_content()  # 回到默认的 html(从iframe回切,回到原页面)

来源:B站视频

 

selenium_简单操作

上一篇:保垒机SSH登录脚本


下一篇:centos6 yum源