selenium

慈云数据 2024-04-01 技术支持 48 0

文章目录

  • Selenium
      • 一、环境搭建
        • 1. 安装selenium库
        • 2. 安装浏览器驱动
        • 二、初始化
          • 1. 启动浏览器驱动
          • 2. 设置等待时间
          • 3. 打开网站
          • 4. 程序的最后关闭浏览器驱动
          • 三、选择元素
            • 1. 导入库
            • 2. 根据ID查找
            • 3. 根据css选择器查找
            • 4. 其他查找方法
            • 5. find_element与find_elements
            • 四、操作元素
              • 1. 输入文字:send_keys()
              • 2. 点击:click()
              • 3. 下拉框选择
              • 五、切换窗口
                • 1. iframe
                • 2. 切换页面
                • 视频地址与完整代码

                  selenium

                  一、环境搭建

                  1. 安装selenium库
                  pip install selenium
                  
                  2. 安装浏览器驱动

                  网页链接:Microsoft Edge Driver - Microsoft Edge Developer

                  selenium
                  (图片来源网络,侵删)

                  简单起见将下载的msedgedriver.exe与py文件放在同一目录下,可以不用配置环境变量

                  二、初始化

                  1. 启动浏览器驱动
                  from selenium import webdriver
                  wd = webdriver.Edge("msedgedriver")
                  
                  2. 设置等待时间
                  wd.implicitly_wait(5)
                  
                  3. 打开网站
                  wd.get("https://www.bv2008.cn/app/user/register.php?type=org")
                  
                  4. 程序的最后关闭浏览器驱动
                  wd.quit()
                  

                  三、选择元素

                  1. 导入库
                  from selenium.webdriver.common.by import By
                  
                  2. 根据ID查找
                  element = wd.find_element(By.ID, 'ID值')
                  
                  3. 根据css选择器查找
                  element = wd.find_element(By.CSS_SELECTOR, 'css选择器')
                  
                  4. 其他查找方法
                  element = wd.find_element(By.CLASS_NAME, 'class属性')
                  element = wd.find_element(By.TAG_NAME, '标签名')
                  
                  5. find_element与find_elements
                  find_element返回一个元素(第一个)
                  find_elements返回元素列表
                  

                  四、操作元素

                  1. 输入文字:send_keys()
                  element.send_keys('文本')			# 用于文本框
                  
                  2. 点击:click()
                  element.click()			# 用于按钮、超链接、单选框、多选框、
                  
                  3. 下拉框选择
                  s = Select(element)
                  s.select_by_value('value值')
                  # s.select_by_visible_text('文本信息')
                  

                  五、切换窗口

                  1. iframe
                  # 进入到iframe
                  wd.switch_to.frame('iframe名')
                  # wd.switch_to.frame(element)
                  # 回到原页面
                  wd.switch_to.default_content()
                  
                  2. 切换页面
                  for window_handle in wd.window_handles:
                      wd.switch_to.window(window_handle)
                      if '内容' in wd.title:
                          break
                  

                  视频地址与完整代码

                  selenium web自动化,python全自动操作浏览器,适合初学者了解学习

                  selenium
                  (图片来源网络,侵删)
                  from selenium import webdriver
                  from selenium.webdriver.common.by import By
                  from selenium.webdriver.support.select import Select
                  wd = webdriver.Edge("msedgedriver")
                  wd.implicitly_wait(10)
                  wd.get("https://www.bv2008.cn/app/user/register.php?type=org")
                  wd.find_element(By.ID, 'login_name').send_keys('abcde123')
                  wd.find_element(By.CSS_SELECTOR, '#reg_org > table > tbody:nth-child(1) > tr:nth-child(10) >'
                                                   ' td:nth-child(2) > input[type=radio]:nth-child(2)').click()
                  # Select(wd.find_element(By.ID, 'org_type1')).select_by_value('1727')
                  Select(wd.find_element(By.ID, 'org_type1')).select_by_visible_text('社会服务机构')
                  wd.find_element(By.CSS_SELECTOR, '#org_record1 > tr:nth-child(2) > td:nth-child(2) > a').click()
                  wd.switch_to.frame(wd.find_element(By.CSS_SELECTOR, 'body > div.div_tips > iframe'))
                  wd.find_element(By.CSS_SELECTOR, '#org_list > ul > li > span.son > ul > li:nth-child(1) > a:nth-child(3)').click()
                  wd.switch_to.default_content()
                  wd.find_element(By.CSS_SELECTOR, 'body > div.nav > div > a:nth-child(2)').click()
                  wd.find_element(By.CSS_SELECTOR, 'body > div.main > div:nth-child(1) > ul > li:nth-child(2) > div > a').click()
                  for window_handle in wd.window_handles:
                      wd.switch_to.window(window_handle)
                      if '助力' in wd.title:
                          break
                  wd.find_element(By.CSS_SELECTOR, '#main_body > div.wrap.clearfix.m10 > div.conr >'
                                                   ' div:nth-child(2) > table > tbody > tr > td:nth-child(2) > a').click()
                  # wd.quit()
                  
微信扫一扫加客服

微信扫一扫加客服

点击启动AI问答
Draggable Icon