赞
踩
from selenium import webdriver
browser = webdriver.Chrome()
# 访问网站
url = 'https://www.jd.com'
browser.get(url)
""" selenium基本使用 Author:binxin Date:2023/11/29 14:42 """ # 导入 from selenium import webdriver # 创建浏览器操作对象 browser = webdriver.Chrome() # 访问网站 url = 'https://www.jd.com' browser.get(url) # page_source获取网页源码 content = browser.page_source print(content)
from selenium.webdriver.common.by import By
inputTag = driver.find_element(By.ID, "value")
inputTag = driver.find_element(By.CLASS_NAME, "value")
inputTag = driver.find_element(By.NAME, "value")
inputTag = driver.find_element(By.TAG_NAME, "value")
inputTag = driver.find_element(By.XPATH, "value")
inputTag = driver.find_element(By.CSS_SELECTOR, "value")
""" selenium 元素定位 Author:binxin Date:2023/11/29 15:05 """ from selenium import webdriver from selenium.webdriver.common.by import By browser = webdriver.Chrome() url = 'https://www.baidu.com' browser.get(url) # 元素定位 # 根据ID找到对象 # button = browser.find_element(By.ID,'su') # 根据标签属性的属性值获取对象 # button = browser.find_element(By.NAME, 'wd') # 根据xpath语句获取对象 # button = browser.find_element(By.XPATH, '//input[@id="su"]') # 根据标签名获取对象 # button = browser.find_element(By.TAG_NAME, 'input') # 使用bs4的语法获取对象 # button = browser.find_element(By.CSS_SELECTOR, '#su') button = browser.find_element(By.LINK_TEXT, '新闻') print(button)
.get_attribute('class')
.txt
.tag_name
""" 元素信息 Author:binxin Date:2023/11/29 16:31 """ from selenium import webdriver from selenium.webdriver.common.by import By browser = webdriver.Chrome() url = 'https://www.baidu.com' browser.get(url) input = browser.find_element(By.ID, 'su') # 获取标签的属性 print(input.get_attribute('class')) # 获取标签名 print(input.tag_name) a = browser.find_element(By.LINK_TEXT, '新闻') # 获取元素文本 print(a.text)
click()
send_keys()
browser.back()
browser.forward()
js='document.documentElement.scrollTop=100000'
browser.execute_script(js) # 执行js代码
page_source
browser.quit()
""" selenium交互 Author:binxin Date:2023/11/29 16:47 """ from selenium import webdriver from selenium.webdriver.common.by import By browser = webdriver.Chrome() url = 'https://www.baidu.com' browser.get(url) import time time.sleep(2) # 获取文本框对象 input = browser.find_element(By.ID, 'kw') # 在文本框中输入周杰伦 input.send_keys('周杰伦') time.sleep(2) # 获取百度一下按钮 button = browser.find_element(By.ID, 'su') # 点击按钮 button.click() time.sleep(2) # 滑动窗口到底部 js_bottom = 'document.documentElement.scrollTop=100000' browser.execute_script(js_bottom) time.sleep(2) # 获取下一页按钮 next = browser.find_element(By.XPATH, '//a[@class="n"]') # 点击下一页 next.click() time.sleep(2) # 返回上一页 browser.back() time.sleep(2) # 重新回到上一页 browser.forward() time.sleep(3) # 退出 browser.quit()
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
browser = webdriver.Chrome(options=chrome_options)
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def share_browser():
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
browser = webdriver.Chrome(options=chrome_options)
return browser
""" Chrome handless Author:binxin Date:2023/11/30 11:06 """ from selenium import webdriver from selenium.webdriver.chrome.options import Options def share_browser(): chrome_options = Options() chrome_options.add_argument('--headless') chrome_options.add_argument('--disable-gpu') browser = webdriver.Chrome(options=chrome_options) return browser # 封装调用 browser = share_browser() url = 'http://www.baidu.com/' browser.get(url) browser.save_screenshot('baidu.png')
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。