首页 > 其他 > 详细

2019 7.8学习笔记

时间:2019-07-08 10:03:58      阅读:91      评论:0      收藏:0      [点我收藏+]

行为链

有时候在页面中的操作可能要有很多步,这时候可以通过使用鼠标行为链类ActionChains来完成。

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

driver_path=r"E:\chromedriver\chromedriver.exe"
driver=webdriver.Chrome(executable_path=driver_path)
driver.get(http://www.baidu.com)

inputTag=driver.find_element_by_id(kw)
submitTag=driver.find_element_by_id(su)

actions=ActionChains(driver)
actions.move_to_element(inputTag)
actions.send_keys_to_element(inputTag,python)
actions.move_to_element(submitTag)
actions.click(submitTag)
actions.perform()
# 更多的鼠标相关的操作
# click_and_hold(element)     点击但不松开鼠标
# context_click(element)      右键点击
# double_click(element)       双击

Cookie操作

from selenium import webdriver

driver_path=r"E:\chromedriver\chromedriver.exe"
driver=webdriver.Chrome(executable_path=driver_path)
driver.get(http://www.baidu.com)

for cookie in driver.get_cookies():  #获取所有的cookie
    print(cookie)
print(-*20)
print(driver.get_cookie(delPer))   #获取某一个cookie
print(-*20)
driver.delete_cookie(delPer)       #删除某一个cookie
print(driver.get_cookie(delPer))
print(-*20)
driver.delete_all_cookies()          #删除所有的cookie

 隐式等待和显示等待

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
driver_path=r"E:\chromedriver\chromedriver.exe"
driver=webdriver.Chrome(executable_path=driver_path)
driver.get(http://www.douban.com)

# driver.implicitly_wait(20)          #隐式等待
# driver.find_element_by_id("jhasjkdhkjahsdkhaksldhalkshdj")

# element=WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID,"sdasfafwawf")))
# print(element)
element=WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID,"anony-nav")))           #显示等待
print(element)

 打开多窗口和切换窗口

from selenium import webdriver

driver_path=r"E:\chromedriver\chromedriver.exe"
driver=webdriver.Chrome(executable_path=driver_path)
driver.get(http://www.douban.com)

driver.execute_script("window.open(‘https://www.baidu.com‘)")  #打开新窗口
print(driver.current_url)
driver.switch_to.window(driver.window_handles[1])    #切换窗口
print(driver.current_url)

# 虽然在窗口中切换到了新的页面,但是driver中还没有切换。
# 如果想要在代码中切换新的窗口,并且做一些爬虫
# 那么应该使用driver.switch_to.window来切换到指定的窗口
# 从driver.window_handles中取出具体第几个窗口
# driver.window_handles是一个列表,里面装的都是窗口句柄
# 他会按照打开页面的顺序来存储窗口的句柄

设置代理ip

from selenium import webdriver

driver_path=r"E:\chromedriver\chromedriver.exe"
options=webdriver.ChromeOptions()
options.add_argument("--proxy-server=http://139.180.210.213:9999")
driver=webdriver.Chrome(executable_path=driver_path,chrome_options=options)
driver.get("http://httpbin.org/ip")
#存在部分问题

WebElement元素

from selenium import webdriver

driver_path=r"E:\chromedriver\chromedriver.exe"
driver=webdriver.Chrome(executable_path=driver_path)
driver.get(http://www.baidu.com)

submitBtn=driver.find_element_by_id(su)
print(type(submitBtn))
print(submitBtn.get_attribute("value"))  #返回元素的值
driver.save_screenshot(baidu.png)    #截图

 

2019 7.8学习笔记

原文:https://www.cnblogs.com/jyjoker/p/11149056.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!