python selenium

发布时间:2018-11-08 19:55:35 阅读:947次

http://www.cnblogs.com/zhaof/p/6953241.html

https://blog.csdn.net/eastmount/article/details/48108259

元素操作

https://github.com/mozilla/geckodriver/releases

http://www.cnblogs.com/hanxiaobei/p/6108677.html

https://www.cnblogs.com/kgdxpr/p/6165100.html

pip install selenium

https://blog.csdn.net/qq_35444750/article/details/79619964

https://blog.csdn.net/qq_41714308/article/details/79440618

yum install firefox

https://blog.csdn.net/wailaizhu/article/details/60760120

https://www.cnblogs.com/sandysun/p/7838113.html

from selenium import webdriver # 导入webdriver包

import time
#driver = webdriver.Firefox() # 初始化一个火狐浏览器实例:driver

driver = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver')

driver.maximize_window() # 最大化浏览器 
time.sleep(5) # 暂停5秒钟

driver.get("https://www.baidu.com") # 通过get()方法,打开一个url站点

模拟输入关键词点击百度一下按钮

[root@lnmp pythonscrapy]# cat baidu.py 
#coding:utf-8
#http://www.cnblogs.com/jdy113/p/8034771.html
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
print "百度"
browser = webdriver.Firefox()
browser.get('http://www.baidu.com')
print(browser.title)
assert "百度".decode('utf-8') in browser.title
elem = browser.find_element_by_id("kw")
#elem.send_keys(('今日热点').decode('utf-8'))
elem.send_keys(u'热点')
elem.send_keys(Keys.RETURN)
time.sleep(3)
browser.close()

取所有的链接

[root@lnmp pythonscrapy]# cat href.py 
#https://blog.csdn.net/u011541946/article/details/70140812
# coding=utf-8
import time
from selenium import webdriver
driver = webdriver.Firefox()
driver.maximize_window()
driver.implicitly_wait(6)
driver.get("https://www.baidu.com")
time.sleep(1)
for link in driver.find_elements_by_xpath("//*[@href]"):
    print (link.get_attribute('href'))
driver.quit()

打开百度音乐

[root@lnmp pythonscrapy]# cat test.py 
from selenium import webdriver 
import time
print("hello the world")
driver = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver') 
driver.maximize_window() 
time.sleep(5) 
driver.get("https://music.taihe.com") 
打开淘宝

[root@lnmp pythonscrapy]# cat  taobao.py
from selenium import webdriver
#browser = webdriver.Chrome()
browser = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver') 
browser.get("http://www.taobao.com")
input_first = browser.find_element_by_id("q")
input_second = browser.find_element_by_css_selector("#q")
input_third = browser.find_element_by_xpath('//*[@id="q"]')
print(input_first)
print(input_second)
print(input_third)
browser.close()
[root@lnmp pythonscrapy]# cat zhifu.py 
from selenium import webdriver
#browser = webdriver.Chrome()
browser = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver') 
url = 'https://www.zhihu.com/explore'
browser.get(url)
input = browser.find_element_by_class_name('zu-top-add-question')
print(input.text)
[root@lnmp pythonscrapy]# 
[root@lnmp pythonscrapy]# cat class.py
from selenium import webdriver
#browser = webdriver.Chrome()
browser = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver') 
url = 'https://www.zhihu.com/explore'
browser.get(url)
logo = browser.find_element_by_id('zh-top-link-logo')
print(logo)
print(logo.get_attribute('class'))
[root@lnmp pythonscrapy]# cat link.py
from selenium import webdriver
browser = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver') 
url = 'https://www.zhihu.com/explore'
browser.get(url)
urls = browser.find_elements_by_xpath("//a")
for url in urls:
    print(url.get_attribute("href"))
#links = browser.find_element_by_tag_name("a")
#link = links[1]
#link.click()
[root@lnmp pythonscrapy]# cat lizi.html
<select name="cars" onchange="alert(this.value);">
<option value=1>one</option>
<option value=2>two</option>
<option value=3>three</option>
</selecT>
[root@lnmp pythonscrapy]# cat html.html
<html>
<head>
        <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
        <title>Checkbox</title>
        <link href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet"/>
        <script src="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.js"></script>
</head>
<body>
        <h3>checkbox</h3>
        <div class="well">
                <form class="form-horizontal">
                        <div class="control-group">
                                <label class="control-label" for="cl">checkbox1</label>
                                <div class="controls">
                                        <input type="checkbox" id="c1"/>
                                </div>
                        </div>
                        <div class="control-group">
                                <label class="control-label" for="c2">checkbox2</label>
                                <div class="controls">
                                        <input type="checkbox" id="c2"/>
                                </div>
                        </div>
                        <div class="control-group">
                                <label class="control-label" for="c3">checkbox3</label>
                                <div class="controls">
                                        <input type="checkbox" id="c3"/>    
                                </div>
                        </div>
                        <input type="text" id="username"/>
                </form>
        </div>
</body>
</html>
[root@lnmp pythonscrapy]# cat element.py
#coding:utf-8
from selenium import webdriver
from time import *
bro = webdriver.Firefox()
bro.maximize_window()
bro.get("file:///var/www/html/python/pythonscrapy/lizi.html")
sleep(1)
#二次定位使用tag_name,若不能直接定位元素时,则需要先定位上一级元素标签,然后再进行定位
# bro.find_element_by_tag_name('select').find_element_by_id("3").click()
a = bro.find_element_by_tag_name('select').find_elements_by_tag_name("option")#注意后面标签是一个元素组需要用elements
for i in a: #循环点击,每循环一次就对a进行操作
    #print(i)
    i.click()
    sleep(1)
[root@lnmp pythonscrapy]# cat get.py
#https://blog.csdn.net/u012605082/article/details/80517352
from selenium import webdriver
import os,time
driver = webdriver.Firefox()
file_path = "file:///var/www/html/python/pythonscrapy/html.html"
print(file_path)
driver.get(file_path)
inputs = driver.find_elements_by_tag_name("input")
for i in inputs:
    if i.get_attribute("type") == "checkbox":
        i.click()
        time.sleep(1)
driver.quit()
[root@lnmp pythonscrapy]# cat baidutest.py
#coding=utf-8
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.baidu.com")
# 返回百度页面底部备案信息
text = driver.find_element_by_id("cp").text
print(text)
driver.close()
[root@lnmp pythonscrapy]# cat href.py
#https://blog.csdn.net/u011541946/article/details/70140812
# coding=utf-8
import time
from selenium import webdriver
driver = webdriver.Firefox()
driver.maximize_window()
driver.implicitly_wait(6)
driver.get("https://www.baidu.com")
time.sleep(1)
for link in driver.find_elements_by_xpath("//*[@href]"):
    print (link.get_attribute('href'))
driver.quit()

[root@lnmp pythonscrapy]# cat qq.py 
from selenium import webdriver  
from selenium.webdriver.common.keys import Keys  
import time
# Login 163 email
driver = webdriver.Firefox()  
driver.get("http://mail.shzm.com/")
elem_user = driver.find_element_by_name("qquin")
elem_user.clear
elem_user.send_keys("yansiyu")  
elem_pwd = driver.find_element_by_name("pp")
elem_pwd.clear
elem_pwd.send_keys("123456")  
elem_pwd.send_keys(Keys.RETURN)
driver.find_element_by_id("ss").click()
driver.find_element_by_id("btlogin").click()
driver.find_element_by_id("btlogin").submit()
[root@lnmp pythonscrapy]# 

如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术

支付宝 微信

有疑问联系站长,请联系QQ:QQ咨询

转载请注明:python selenium 出自老鄢博客 | 欢迎分享