from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
from time import sleep
# web=Chrome()
# web.maximize_window()
# web.get(‘http://lagou.com‘)
# # click() 点击事件
# web.find_element_by_xpath(‘//*[@id="changeCityBox"]/p[1]/a‘).click()
# #元素定位 Keys.ENTER回车
# sleep(1)
# el= web.find_element_by_xpath(‘//*[@id="search_input"]‘).send_keys(‘python‘,Keys.ENTER)
# sleep(1)
# # find_elements_by_xpath 获取所有的li
# # list_li = web.find_elements_by_xpath(‘//*[@id="s_position_list"]/ul/li‘)
# web.find_element_by_xpath(‘//*[@id="s_position_list"]/ul/li[1]/div[1]/div[1]/div[1]/a/h3‘).click()
# # for li in list_li:
# # title=li.find_element_by_tag_name(‘h3‘).text
# # money = li.find_element_by_xpath("div[1]/div[1]/div[2]/div/span").text
# # print(title,money)
# # 切换窗口
# web.switch_to.window(web.window_handles[-1])
# sleep(1)
# # 在新窗口拿东西
# cont = web.find_element_by_xpath(‘//*[@id="job_detail"]/dd[2]/div‘).text
# print(cont)
# # 关闭子窗口
# web.close()
# # 回到原来的窗口
# web.switch_to.window(web.window_handles[0])
#########################################################
# web=Chrome()
# web.get("https://www.91kanju.com/vod-play/17873-1-1.html")
# 拿属性值
# iframe = web.find_element_by_xpath(‘//*[@id="cms_player"]/div[2]/video‘)
# a=iframe.get_attribute(‘src‘)
# https://www.91kanju.com/vod-play/1658-1-1.html
# web=Chrome()
# web.get("https://www.91kanju.com/vod-play/1658-1-1.html")
# 处理iframe 需要先切换进去然后在出来拿数据
# 先定位iframe 然后进去
# iframe = web.find_element_by_xpath(‘//*[@id="player_iframe"]‘)
# web.switch_to.frame(iframe)
# a=web.find_element_by_xpath(‘/html/head/meta[3]‘).get_attribute(‘content‘)
# # 切回iframe
# # web.switch_to.default_content()
#
# #print(a)
################## 无头浏览器 ###############################
# 加载下拉框用到的模块
from selenium.webdriver.support.select import Select
# 无头导包
from selenium.webdriver.chrome.options import Options
# 准备参数
chrome_options = Options()
#后面的两个是固定写法 必须这么写
chrome_options.add_argument(‘--headless‘)
chrome_options.add_argument(‘--disable-gpu‘)
# web = Chrome(chrome_options=chrome_options) # 把参数配置设置到浏览器
web = Chrome(options=chrome_options) # 新版把参数配置设置到浏览器
web.get(‘https://www.endata.com.cn/BoxOffice/BO/Year/index.html‘)
# # 定位到下拉列表
# sel = web.find_element_by_xpath(‘//*[@id="OptionDate"]‘)
# #对元素进行包装,包装成下拉菜单
# sel = Select(sel)
# 调整选效
# for i in range(len(sel.options)): # i就是每个下拉框的索引
# sel.select_by_index(i) # 按照索引取值
sleep(2)
# title=web.find_element_by_xpath(‘//*[@id="TableList"]/table/tbody/tr[1]/td[2]/a/p‘).text
# print(title)
#
# web.close()
# 拿到页面源代码(经过数据加载以及js执行之后的结果的html内容)
# t=web.page_source
# # print(t)
# # web.close()
原文:https://www.cnblogs.com/thaimj1314520/p/14672405.html