当前位置:   article > 正文

爬爬爬——今天是浏览器窗口切换和给所选人打钩(自动化)

爬爬爬——今天是浏览器窗口切换和给所选人打钩(自动化)

学习爬虫路还很长,第一阶段花了好多天了,还在底层,虽然不是我专业要学习的语言,和必备的知识,但是我感觉还挺有意思的。加油,这两天把建模和ai也不学了,唉过年了懒了!

加油坚持就是胜利!!!

先是浏览器窗口切换,这都是给浏览器驱动器发送指令,然后让他去干,很酷!!!!

  1. import time
  2. from selenium import webdriver
  3. from selenium.webdriver.common.by import By
  4. url = "https://cdn2.byhy.net/files/selenium/sample3.html"
  5. wd = webdriver.Edge()
  6. wd.implicitly_wait(10)
  7. wd.get(url)
  8. element = wd.find_element(By.CSS_SELECTOR,'a')
  9. print(element.get_attribute('outerHTML'))
  10. element.click()
  11. #存储下来
  12. mainWindow = wd.current_window_handle
  13. time.sleep(5)
  14. for handle in wd.window_handles:
  15. wd.switch_to.window(handle)
  16. print(wd.title)
  17. if '必应' in wd.title:
  18. break
  19. wd.find_element(By.CSS_SELECTOR,'#sb_form_q').send_keys("hahahah")
  20. time.sleep(1)
  21. wd.find_element(By.CSS_SELECTOR,'#search_icon').click()
  22. time.sleep(5)
  23. #返回窗口
  24. wd.switch_to.window(mainWindow)
  25. wd.find_element(By.CSS_SELECTOR,'button').click()
  26. wd.find_element(By.CSS_SELECTOR,'button').click()
  27. time.sleep(5)
  28. wd.quit()

代码部分:

1.time-给浏览器时间,webdriver建立驱动浏览器,By-寻找(选择)

  1. import time
  2. from selenium import webdriver
  3. from selenium.webdriver.common.by import By

2.

  1. url = "https://cdn2.byhy.net/files/selenium/sample3.html"
  2. wd = webdriver.Edge()
  3. wd.implicitly_wait(10)

建立联系,然后用

wd.implicitly_wait(10)———如果没有返回就等待,最多10s

3.

  1. element = wd.find_element(By.CSS_SELECTOR,'a')
  2. print(element.get_attribute('outerHTML'))
  3. element.click()

找到标签为‘a’,打印‘a’在浏览器的代码是什么,打印的就是什么!!

然后点击(click())

4.

mainWindow = wd.current_window_handle

将现在的储存起来,便于操作完返回

5.

  1. for handle in wd.window_handles:
  2. wd.switch_to.window(handle)
  3. print(wd.title)
  4. if '必应' in wd.title:
  5. break

在浏览器的窗口栏找到有‘必应’这两个字的,然后选择

6.搜索‘hahah’

  1. wd.find_element(By.CSS_SELECTOR,'#sb_form_q').send_keys("hahahah")
  2. time.sleep(1)
  3. wd.find_element(By.CSS_SELECTOR,'#search_icon').click()

7.

  1. wd.switch_to.window(mainWindow)
  2. wd.find_element(By.CSS_SELECTOR,'button').click()
  3. wd.find_element(By.CSS_SELECTOR,'button').click()
  4. time.sleep(5)

然后返回最初的窗口

结束!!!


                                                        --------- 选择框-----

radio,checkbox,select框的选择

代码:

  1. import time
  2. from selenium.webdriver.support.ui import Select
  3. from selenium import webdriver
  4. from selenium.webdriver.common.by import By
  5. url = "https://cdn2.byhy.net/files/selenium/test2.html"
  6. wd = webdriver.Edge()
  7. wd.implicitly_wait(5)
  8. wd.get(url)
  9. time.sleep(1)
  10. elements = wd.find_element(By.CSS_SELECTOR,'#s_radio input[checked=checked]')
  11. #print(elements.get_attribute("outerHTML"))
  12. #print('您选的是:'+elements.get_attribute('value'))
  13. #选择 ---------------------------radio
  14. wd.find_element(By.CSS_SELECTOR,'#s_radio input[value="小雷老师"]').click()
  15. element = wd.find_element(By.CSS_SELECTOR,'#s_radio input[checked=checked]')
  16. time.sleep(1)
  17. #选择---------------------------checkbox---多选
  18. elements2 = wd.find_elements(By.CSS_SELECTOR,'#s_checkbox input[checked=checked]')
  19. for i in elements2:
  20. i.click()
  21. time.sleep(1)
  22. wd.find_element(By.CSS_SELECTOR,'#s_checkbox input[value="小雷老师"]').click()
  23. wd.find_element(By.CSS_SELECTOR,'#s_checkbox input[value="小凯老师"]').click()
  24. time.sleep(1)
  25. #选择---------------------------select---滑动
  26. select = Select(wd.find_element(By.CSS_SELECTOR,'#ss_single'))
  27. select.select_by_visible_text('小雷老师')
  28. time.sleep(1)
  29. #多选
  30. select2 = Select(wd.find_element(By.CSS_SELECTOR,'#ss_multi'))
  31. #删除所有
  32. select2.deselect_all()
  33. select2.select_by_visible_text('小雷老师')
  34. select2.select_by_visible_text('小凯老师')
  35. time.sleep(1)
  36. wd.quit()

看着比较多,但是就是几个重复用。

代码:

1.

  1. elements = wd.find_element(By.CSS_SELECTOR,'#s_radio input[checked=checked]')
  2. #print(elements.get_attribute("outerHTML"))
  3. #print('您选的是:'+elements.get_attribute('value'))
  4. ‘’

找到你现在目前所选的。

2.radio框

  1. #选择 ---------------------------radio
  2. wd.find_element(By.CSS_SELECTOR,'#s_radio input[value="小雷老师"]').click()
  3. element = wd.find_element(By.CSS_SELECTOR,'#s_radio input[checked=checked]')
  4. time.sleep(1)

找到你要选的,然后点击它,

3.多选

  1. #选择---------------------------checkbox---多选
  2. elements2 = wd.find_elements(By.CSS_SELECTOR,'#s_checkbox input[checked=checked]')
  3. for i in elements2:
  4. i.click()
  5. time.sleep(1)
  6. wd.find_element(By.CSS_SELECTOR,'#s_checkbox input[value="小雷老师"]').click()
  7. wd.find_element(By.CSS_SELECTOR,'#s_checkbox input[value="小凯老师"]').click()
  8. time.sleep(1)

找到点击了的,然后在点一次,就是全部清除了。

然后选择

4. select滑动

  1. #选择---------------------------select---滑动
  2. select = Select(wd.find_element(By.CSS_SELECTOR,'#ss_single'))
  3. select.select_by_visible_text('小雷老师')
  4. time.sleep(1)

找到对应的,然后用文本搜索:选择

5.

  1. #多选
  2. select2 = Select(wd.find_element(By.CSS_SELECTOR,'#ss_multi'))
  3. #删除所有
  4. select2.deselect_all()
  5. select2.select_by_visible_text('小雷老师')
  6. select2.select_by_visible_text('小凯老师')

多选的,就是全部删除,然后用文本选择


学习与白月黑羽up主,爆赞!!!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/115508
推荐阅读