#(1)导入selenium
from selenium import webdriver
path = 'chromedriver.exe'
browser = webdriver.Chrome(path)
#(3)访问网站
# url = 'https://www.baidu.com'
# browser.get(url)
url = 'https://www.jd.com' #可以访问到电商里面的秒杀板块
browser.get(url)
#page_source获取网页源码
content = browser.page_source
print(content)
from selenium import webdriver
from selenium.webdriver.common.by import By
path = 'chromedriver.exe'
browser = webdriver.Chrome(path)
url = 'https://www.baidu.com'
browser.get(url)
#元素定位
#根据id来找对象
# button = browser.find_element_by_id('su')
# print(button)
#根据标签属性的属性值来获取对象
# button = browser.find_element_by_name('wd')
# print(button)
#根据xpath语句来获取对象
# button = browser.find_element_by_xpath('//input[@id="su"]') #过时了
# button = browser.find_element(by=By.XPATH,value='//input[@id="su"]')
# print(button)
#根据标签的名字来获取对象
# button = browser.find_element(by=By.TAG_NAME,value='input')
# # button = browser.find_element_by_tag_name('input')
# print(button)
#使用的bs4的语法来获取对象
# button = browser.find_element(by=By.CSS_SELECTOR,value='#su')
# print(button)
# button = browser.find_element_by_link_text('新闻')
button = browser.find_element(by=By.LINK_TEXT,value='新闻')
print(button)
(1)元素信息
from selenium import webdriver
from selenium.webdriver.common.by import By
path = 'chromedriver.exe'
browser = webdriver.Chrome(path)
url = 'http://www.baidu.com'
browser.get(url)
input = browser.find_element(by=By.ID,value='su')
#获取标签的属性
print(input.get_attribute('class'))
#获取标签的名字
print(input.tag_name)
#获取元素文本
a = browser.find_element(by=By.LINK_TEXT,value='新闻')
print(a.text)
(2)交互
from selenium import webdriver
from selenium.webdriver.common.by import By
path = 'chromedriver.exe'
browser = webdriver.Chrome(path)
url = 'https://www.baidu.com'
browser.get(url)
import time
time.sleep(2)
#获取文本框的对象
input = browser.find_element(by=By.ID,value='kw')
#在文本框内输入周杰伦
input.send_keys('周杰伦')
time.sleep(2)
#获取“百度一下”按钮
button = browser.find_element(by=By.ID,value='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=By.XPATH,value='//a[@class="n"]')
next.click()
time.sleep(2)
#回到上一页
browser.back()
time.sleep(2)
#回去
browser.forward()
time.sleep(3)
#退出
browser.quit()
#phantomjs已经被淘汰,推荐handless——>无界面浏览器
# phantomjs和handless都是基于selenium的
# 现在推荐使用 handless
from selenium import webdriver
path = 'phantomjs.exe'
browser = webdriver.PhantomJS(path)
url = 'https://www.baidu.com'
browser.get(url)
# from selenium import webdriver
# from selenium.webdriver.chrome.options import Options
#
# chome_options = Options()
# chome_options.add_argument('--headless')
# chome_options.add_argument('--disable-gpu')
#
# path = r'C:\Program Files\Google\Chrome\Application\chrome.exe'
# chome_options.binary_location = path
#
# browser = webdriver.Chrome(chrome_options=chome_options)
# url = 'http://www.baidu.com'
# browser.get(url)
# browser.save_screenshot('baidu.png')
#封装的handless
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')
#path是你自己的chrome浏览器的文件路径
path = r'C:\Program Files\Google\Chrome\Application\chrome.exe'
chrome_options.binary_location = path
browser = webdriver.Chrome(chrome_options=chrome_options)
return browser
browser = share_browser()
url = 'https://www.baidu.com'
browser.get(url)