from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
time.sleep(5)
driver.close()
- 找元素 看那个舒服 就重点多练一下 不过有时候 要注意 是不是列表!! ! !
'''
selenium定位元素:
1. find_element_by_id:根据id来查找某个元素。
2. find_element_by_class_name:根据类名查找元素。
3. find_element_by_name:根据name属性的值来查找元素。
4. find_element_by_tag_name:根据标签名来查找元素。
5. find_element_by_xpath:根据xpath语法来获取元素。
6. find_element_by_css_selector:根据css选择器选择元素。
要注意,find_element是获取第一个满足条件的元素。find_elements是获取所有满足条件的元素。
'''
from selenium import webdriver
driver=webdriver.Chrome()
driver.get('http://www.baidu.com')
input=driver.find_elements_by_tag_name('input')[7]
input.send_keys('python')
'''
1. webelement.send_keys:给输入框填充内容。
2. webelement.click:点击。
3. 操作select标签:需要首先用`from selenium.webdriver.support.ui import Select`来包装一下选中的对象,才能进行`select`选择:
* select_by_index:按索引进行选择。
* select_by_value:按值进行选择。
* select_by_visible_text:按照可见文本进行选择。
'''
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
driver=webdriver.Chrome()
input=driver.get('https://www.zhihu.com/signin?next=%2F')
username=driver.find_element_by_name('username')
username.send_keys('15213227496')
password=driver.find_element_by_name('digits')
password.send_keys('1234')
submit=driver.find_element_by_xpath('//div[@class="Login-options"]/button')
submit.click()
driver.get("https://accounts.douban.com/passport/login_popup?login_source=anony")
checkbox = driver.find_element_by_name("remember")
checkbox.click()
driver.get("http://www.nchu.edu.cn/")
select = Select(driver.find_element(By.ID,"ContentPlaceHolder1_dpLinkList"))
select.select_by_visible_text("东航优惠机票1")
'''
有时候在页面中的操作可能要有很多步,那么这时候可以使用鼠标行为链类selenium.webdriver.common.action_chains import ActionChains来完成。比如现在要将鼠标移动到某个元素上并执行点击事件。
相关的操作:
move_to_element(element) :移动鼠标到元素
send_keys_to_element(element,内容): 传值
click(element): 单击
click_and_hold(element):点击但不松开鼠标。
context_click(element):右键点击。
double_click(element):双击。
更多方法请参考:http://selenium-python.readthedocs.io/api.html
'''
from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains
driver=webdriver.Chrome()
driver.get('https://www.zhihu.com/signin?next=%2F')
actions=ActionChains(driver)
passwordloade_tag=driver.find_elements_by_class_name('SignFlow-tab')[1]
actions.move_to_element(passwordloade_tag)
actions.click(passwordloade_tag)
actions.perform()
input_tag=driver.find_element_by_name('username')
password_tag=driver.find_element_by_name('password')
button_tag=driver.find_element_by_xpath('//form/button')
actions.move_to_element(input_tag)
actions.send_keys_to_element(input_tag,'1888888888')
time.sleep(2)
actions.move_to_element(password_tag)
actions.send_keys_to_element(password_tag,'123456')
time.sleep(3)
actions.move_to_element(button_tag)
actions.click(button_tag)
actions.perform()
'''
1. 获取所有的cookie:
for cookie in driver.get_cookies():
print(cookie)
2. 根据cookie的key获取value: 因为是 字典 模式 key 是键值
value = driver.get_cookie(key)
3. 删除所有的cookie:
driver.delete_all_cookies()
4. 删除某个cookie:
driver.delete_cookie(key)
5. 添加cookie:
driver.add_cookie({“name”:”username”,”value”:”abc”})
'''
from selenium import webdriver
drive=webdriver.Chrome()
drive.get('https://www.baidu.com/?tn=40020637_9_oem_dg')
drive.add_cookie({"name":'zz' ,"uername": 'tt','value':'lll'})
value=drive.get_cookie('zz')
cookies=drive.get_cookies()
drive.delete_cookie('zz')
'''
1. 隐式等待:指定一个时间,在这个时间内一直会处于等待状态。隐式等待需要使用`driver.implicitly_wait`。
2. 显式等待:指定在某个时间内,如果某个条件满足了,那么就不会再等待,如果在指定的时间内条件都不满足,那么就不会再等待了。显式等待用的方法是`from selenium.webdriver.support.ui import WebDriverWait`。示例代码如下:
driver.get("https://kyfw.12306.cn/otn/leftTicket/init?linktypeid=dc")
WebDriverWait(driver,100).until(
EC.text_to_be_present_in_element_value((By.ID,"fromStationText"),"长沙")
)
WebDriverWait(driver,100).until(
EC.text_to_be_present_in_element_value((By.ID,"toStationText"),"北京")
)
btn = driver.find_element_by_id("query_ticket")
btn.click()
'''
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver=webdriver.Chrome()
'''
driver.get('https://www.baidu.com/?tn=40020637_9_oem_dg')
driver.implicitly_wait(5) #等待
driver.find_element_by_id('123')
'''
driver.get('https://www.12306.cn/index/')
bnt_tag=driver.find_element_by_id('search_one')
WebDriverWait(driver,100).until(
EC.text_to_be_present_in_element_value((By.ID,'fromStation'),'长沙')
)
WebDriverWait(driver,100).until(
EC.text_to_be_present_in_element_value((By.ID,'toStation'),'长沙')
)
bnt_tag.click()
'''
1. selenium中没有专门的打开新窗口的方法,是通过`window.execute_script()`来执行`js`脚本的形式来打开新窗口的。
window.execute_script("window.open('https://www.douban.com/')")
2. 打开新的窗口后`driver`当前的页面依然还是之前的,如果想要获取新的窗口的源代码,那么就必须先切换过去。示例代码如下:
window.switch_to.window(driver.window_handlers[1])
'''
from selenium import webdriver
driver=webdriver.Chrome()
driver.get('https://www.baidu.com/?tn=40020637_4_oem_dg')
driver.implicitly_wait(5)
driver.execute_script("window.open('https://www.guazi.com/cq/buy/')")
print(driver.page_source)
driver.switch_to.window(driver.window_handles[1])
print(driver.page_source)
'''
设置代理通过`ChromeOptions`来设置,示例代码如下:
options = webdriver.ChromeOptions()
options.add_argument("--proxy-server=http://110.52.235.176:9999")
driver = webdriver.Chrome(executable_path="D:\ProgramApp\chromedriver\chromedriver73.exe",options=options)
driver.get("http://httpbin.org/ip")
'''
from selenium import webdriver
options=webdriver.ChromeOptions()
options.add_argument('proxy-server=http://171.35.169.254:9999')
driver=webdriver.Chrome(options=options)
driver.get("http://httpbin.org/ip")
'''
### 补充:
1. get_property:获取html标签中官方写好的属性。
2. get_attribute:获取html标签中官方和非官方的属性。
3. driver.save_screenshoot:获取当前页面的截图,有时候请求失败了,那么可以把当前网页的截图保存下来,方便后期进行分析。
driver.save_screenshot('baidu.png')
'''
from selenium import webdriver
import time
driver=webdriver.Chrome()
js="var q=document.documentElement.scrollTop=100000"
driver.execute_script(js)
time.sleep(3)
js="var q=document.documentElement.scrollTop=0"
driver.execute_script(js)
time.sleep(3)
js="var q=document.getElementById('id').scrollTop=100000"
driver.execute_script(js)
time.sleep(3)
from selenium import webdriver
from selenium.webdriver import ChromeOptions
option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
driver=webdriver.Chrome(options=option)
option.add_experimental_option('useAutomationExtension',False)
driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument',{'source':'Object.defineProperty(navigator,"webdriver",{get:()=>undefined})'})
url = 'https://www.zhipin.com/'
driver.get(url)
driver.find_element_by_xpath('//*[@id="wrap"]/div[3]/div/div/div[1]/form/div[2]/p/input').send_keys('Python')
driver.find_element_by_xpath('//*[@id="wrap"]/div[3]/div/div/div[1]/form/button').click()
from selenium import webdriver
from selenium.webdriver import ChromeOptions
option = ChromeOptions()
option.add_argument('--headless')
browser = webdriver.Chrome(options=option)
url = 'https://www.baidu.com'
browser.get(url)
browser.save_screenshot('baidu.png')