admin管理员组

文章数量:1533067

前一篇文章[Python+Selenium+Edge浏览器安装与简单运行(1/2)]介绍了环境的安装,本篇就就介绍下如何控制在Edge浏览器中打开bilibili网站并搜索’日本核废水‘的相关视频。

先放上最终的程序test_webdriver.py:

from time import sleep
from selenium import webdriver

driverfile_path = r'C:\Program Files\Python36\msedgedriver.exe'
one_driver = webdriver.Edge(executable_path=driverfile_path)

one_driver.get(r'https://www.bilibili/')
one_driver.implicitly_wait(10)

search_box = one_driver.find_element_by_xpath("//input[@type='text'][@autocomplete='off']")
search_box.send_keys('日本核废水')

search_button = one_driver.find_element_by_css_selector("[type='button']")
search_button.click()

sleep(5)
one_driver.quit()

程序中主要的操作是对元素的查找,常见的方法包括:
根据id属性查找的select_element_by_id()
根据class属性查找的select_element_by_class_name()
根据tag名称查找的select_element_by_tag_name()
根据css格式查找的select_element_by_css_selector()
根据xpath格式查找的select_element_by_xpath()

Python通过WebDriver操控Edge的知识比较多,建议参考白月黑羽的网页自动化教程。它还有配套的视频教程,讲得非常清晰明了,建议配合观看,效果更佳。

关于对于WebDriver的一些设置,可以参考Python中使用selenium(三)特殊操作。

本文标签: 浏览器简单PythonseleniumEdge