当前位置: 首页> 财经> 访谈 > 金华seo快速排名_html个人博客完整代码_西安网站seo外包_seo网站推广是什么意思

金华seo快速排名_html个人博客完整代码_西安网站seo外包_seo网站推广是什么意思

时间:2025/7/9 6:00:33来源:https://blog.csdn.net/u012111923/article/details/143208887 浏览次数:0次
金华seo快速排名_html个人博客完整代码_西安网站seo外包_seo网站推广是什么意思

 selenium使用

  一 环境搭建

  下载selenium

pip install selenium

下载浏览器驱动(以Edge为例)

在设置中找到当前Edge版本号,在[Microsoft Edge WebDriver - Microsoft Edge Developer](https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/)中下载对应版本驱动。

将下载的压缩包解压后得到驱动的exe文件,将该文件拖到python安装文件夹的script文件夹下。

在浏览器中的使用:

示例

  1.  from selenium.webdriver import Edge # 引入

  2.   web = Edge() # 创建浏览器对象

  3.   web.get("https://www.baidu.com") # 执行操作,打开百度

二 selenium的简单使用

  1. 引入引擎和需要的类

  2. 创建浏览器对象

  3. 打开网页,可以选中元素操纵和获取信息

  示例

  1.  from selenium.webdriver import Edge

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

  3.   import time

  4.   # 创建浏览器对象

  5.   web = Edge()

  6.   # 打开一个网页

  7.   web.get("https://www.lagou.com")

  8.   # 可以使用xpath 类名 样式查找element

  9.   web.find_element('xpath', '//*[@id="changeCityBox"]/p[1]/a').click()

  10.   # 由于加载需要一点时间,所以需要等待

  11.   time.sleep(1)

  12.   web.find_element('xpath', '//*[@id="search_input"]').send_keys('python', Keys.ENTER)

  13.   time.sleep(1)

  14.   web.find_element('xpath', '//*[@id="jobList"]/div[1]/div[1]/div[1]/div[1]/div[1]/a').click()

  15.   time.sleep(1)

  16.   # 切换selenium操作的页面

  17.   web.switch_to.window(web.window_handles[-1])

  18.   job_detail = web.find_element('xpath', '//*[@id="job_detail"]/dd[2]/div').text

  19.   print(job_detail)

  20.   # 关闭当前页面

  21.   web.close()

  22.   # 切回原窗口

  23.   web.switch_to.window(web.window_handles[0])

注意iframe的存在:

  1.  # 如果源码中有iframe的话,是没有办法直接拿到数据的,必须先切换到iframe中再操作

  2.   frame = web.find_element('xpath', '//*[@id="g_iframe"]')

  3.   web.switch_to.frame(frame)

 注意操作select:

  1. 下拉列表应当先引用下拉列表的支持

from selenium.webdriver.support.select import Select

2. 拿到select元素并进行包装

  1.  sel_el = web.find_element_by_xpath('//*[@id="OptionDate"]')

  2.   # 对元素进行包装, 包装成下拉菜单

  3.   sel = Select(sel_el)

  3. 使用条件切换选项,拿到每个选项中的不同数据

  1. for i in range(len(sel.options)): # i就是每一个下拉框选项的索引位置

  2.    sel.select_by_index(i) # 按照索引进行切换

  3.    time.sleep(2)

  4.    table = web.find_element_by_xpath('//*[@id="TableList"]/table')

  5.    print(table.text) # 打印所有文本信息

  6.    print("===================================")

三  无头浏览器

  可以通过参数配置的方式来不打开浏览器也能拿到数据

  如果有被iframe包裹的话需要切换到iframe里面才能拿到数据

  1. from selenium.webdriver import Edge

  2.   from selenium.webdriver.edge.options import Options#引入设置项

  3.   import time

  4.   # 准备好参数配置

  5.   opt = Options()

  6.   opt.add_argument("--headless")

  7.   opt.add_argument("--disable-gpu")

  8.   web = Edge(options=opt) # 把参数配置设置到浏览器中

  9.   web.get("https://music.163.com/#/song?id=1430620302")

  10.   time.sleep(2)

  11.   iframe = web.find_element('xpath', '//*[@id="g_iframe"]')

  12.   web.switch_to.frame(iframe)# 切换到frame中拿取

  13.   # 如何拿到页面代码Elements(经过数据加载以及js执行之后的结果的html内容)

  14.   print(web.page_source)

Options基础配置

  1. user-agent

  2. 代理

  3. 不加载图片和css

  1.  opt = Options()

  2.   # 无头浏览器

  3.   opt.add_argument("--headless")

  4.   opt.add_argument("--disable-gpu")

  5.   # 不加载图片和css

  6.   prefs = {"profile.managed_default_content_settings.images": 2,

  7.    'permissions.default.stylesheet': 2}

  8.   opt.add_experimental_option('prefs', prefs)

  9.   # 设置user-Agent

  10.   opt.add_argument('user-agent=' + UserAgent().random) # 初始化一个别的User-Agent

  11.   # IP池

  12.   proxy_arr = [

  13.    '--proxy-server=http://111.3.118.247:30001',

  14.    '--proxy-server=http://183.247.211.50:30001',

  15.    '--proxy-server=http://122.9.101.6:8888',

  16.   ]

  17.   proxy = random.choice(proxy_arr) # 随机选择一个代理

  18.   print(proxy) # 如果某个代理访问失败,可从proxy_arr中去除

  19.   opt.add_argument(proxy) # 添加代理

  20.   # 添加配置

  21.   driver = Edge(options=opt)

 四 其他实用操作

  如果你的程序被识别到了怎么办?

  1.chrome的版本号如果小于88  在你启动浏览器的时候(此时没有加载任何网页内容), 向页面嵌入js代码. 去掉webdriver

  1. web = Chrome()

  2.   web.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {

  3.    "source": """

  4.    navigator.webdriver = undefined

  5.    Object.defineProperty(navigator, 'webdriver', {

  6.    get: () => undefined

  7.    })

  8.    """

  9.   })

  10.   web.get(xxxxxxx)

 2.chrome的版本大于等于88

  1. option = Options()

  2.   # option.add_experimental_option('excludeSwitches', ['enable-automation'])

  3.   option.add_argument('--disable-blink-features=AutomationControlled')

web操作:

  1. 移动到某一位置点击:

  1. from selenium.webdriver import Edge

  2.   from selenium.webdriver.common.action_chains import ActionChains

  3.   web = Edge()

  4.   verify_img_element = web.find_element_by_xpath('//*[@id="J-loginImg"]')

  5.   ActionChains(web).move_to_element_with_offset(verify_img_element, x, y).click().perform() # perform提交

2. 拖拽:

  1. btn = web.find_element_by_xpath('//*[@id="nc_1_n1z"]')

  2.   ActionChains(web).drag_and_drop_by_offset(btn, 300, 0).perform()

感谢每一个认真阅读我文章的人!!!

作为一位过来人也是希望大家少走一些弯路,如果你不想再体验一次学习时找不到资料,没人解答问题,坚持几天便放弃的感受的话,在这里我给大家分享一些自动化测试的学习资源,希望能给你前进的路上带来帮助。

软件测试面试文档

我们学习必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有字节大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

 

          视频文档获取方式:
这份文档和视频资料,对于想从事【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!以上均可以分享,点下方小卡片即可自行领取。

关键字:金华seo快速排名_html个人博客完整代码_西安网站seo外包_seo网站推广是什么意思

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: