Examples of selenium automated crawling


The most common method of several selenium crawling


find_element_by_id node based on id find
find_element_by_tagname find nodes based on name
find_elements_by_xpath according to find xpath
find_elements_by_class_name tag name to find according to
find_elements_by_css_selector find according to class name
find_elements_by_link_text find links based on the content of

Source offer further ado

from selenium import webdriver
import time
#模拟浏览器对象,如果对象去操作浏览器

path = r'D:\爬虫\selenium\chromedriver.exe';
browser = webdriver.Chrome(executable_path = path)
#print(browser)
url = 'https://www.baidu.com/'
browser.get(url)
 
 
#查找input输入框
my_input = browser.find_element_by_id('kw')
#往里面写文字
my_input.send_keys('美女图片')
 
#点击搜索
#方法一:my_input.send_keys(Keys.TENTER) 
#方法二:找到搜索按钮
button = browser.find_elements_by_tag_name('bg s_btn')
time.sleep(3)
img = browser.find_elements_by_class_name('op-img-address-link-imgs')[0]
img.click()
time.sleep(3)
browser.quit()
  • Note find_elements_by_tag_name () [] If this class on a whole page, so do not []
  • If you enjoy the picture as there are more class, then add!
  • If the code can run yesterday, today it wrong, do not worry, it is likely that your browser version is automatically updated, resulting in your driverChrome do not match. This time to upgrade my Google drive driverChrome on the line
Published 18 original articles · won praise 0 · Views 145

Guess you like

Origin blog.csdn.net/qq_40844663/article/details/103981279