码迷,mamicode.com
首页 > 其他好文 > 详细

selenium的一些操作

时间:2019-07-12 11:29:48      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:color   com   设置   pat   关闭   from   浏览器   second   until   

 

 

1.用selenium中的selenium.webdriver()申明对象

1 ‘’‘申明浏览器对象’‘’
2 
3 from selenium import webdriver
4 browser = webdriver.Chrome()    #创建一个空浏览器
5 browser.get(https://www.baidu.com)     #访问网址
6 browser.close()               #关闭浏览器

2.对文本的相关节点进行查找

1 from selenium import webdriver
2 
3 browser = webdriver.Chrome()
4 browser.get(https://www.taobao.com)
5 input_first = browser.find_element_by_id(q)
6 input_second = browser.find_element_by_css_selector(#q)
7 input_third = browser.find_element_by_xpath(//*[@id = q])
8 print(input_first,input_second,input_third)
9 browser.close()

备注,可以导入 from selenium.webdriver.commom.by import By
然后查找语句可以改变为
input = browser.find_element((By.ID,‘q‘))

如果要查找所有元素,可以设置为find_elements

3.用selenium对浏览器进行相应的操作

 1 from selenium import webdriver
 2 import time  
 3 
 4 browser = webdriver.Chrome()
 5 browser.get(https://www.taobao.com)
 6 input =  browser.find_element_by_id(q)
 7 input.send_keys(iphone)
 8 time.sleep(1)
 9 input.clear()
10 input.send_keys(ipad)
11 button = browser.find_element_by_class_name(btn-search)
12 button.click()

4.拖拽网页中的一些内容布局

from selenium import webdriver
import time
form selenium.webdriver.commom.by import By
from selenium.webdriver import ActionChains


browser = webdriver.Chrome()
browser.get(http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable)

browser.switch_to.frame(iframeResult)
source = browser.find_element_by_css_selector(#draggable)
target = browser.find_element_by_css_selector(#droppable)

action=ActionChains(browser)
actions = action.drag_and_drop(source,target)
actions.perform()

5.对下滑操作,利用javascript来实现

 1 from selenium import webdrive
 2 
 3 
 4 
 5 browser = webdriver.Chrome()
 6 
 7 browser.get(https://www.zhihu.com/explore)
 8 
 9 browser.execute_script(widow_scollTo(0,document.body.scrollHeight))
10 
11 browser.execute_script(alet(To bottom))

6.利用get_attribute()

from selenium import webdriver



browser = webdriver.Chrome()

browser.get(https://www.zhihu.com/explore)
logo = browser.find_element_by_id(zh-top-link-logo)
print(logo)
print(logo.get_attribute(class))

7.获取文本值

1 from selenium import webdriver
2 
3 browser = webdriver.Chrome()
4 url = https://www.zhihu.com/explore
5 browser.get(url)
6 input = browser.find_element_by_class_name(zu-top-add-question)
7 pritn(input.text)

8.获取id、位置、标签名和大小

1 from seleniun import webdriver
2 
3 browser = webdriver.Chrome()
4 browser.get(https://www.zhihu.com/explore)
5 input = browser.find_element_by_class_name(zu-top-add-question)
6 print(input.id)
7 print(input.location)
8 print(input.tag_name)
9 print(input.size)

9.如果页面中有子frame,需要切换到子frame才能使用

 1 import time
 2 from selenium import webdriver
 3 from selenium.commom.exceptions import NoSuchElementException
 4 
 5 browser.get(url)
 6 browser.switch_to.frame(iframeResult)
 7 try:
 8    logo =  browser.find_element_by_class_name(logo)
 9 except NoSuchElementException:
10     print(Not Logo)
11 browser.switch_to.parent_frame()
12 logo = browser.find_element_by_class_name(logo)
13 print(logo)
14 print(logo.text)

10.隐式等待,显示等待

隐式等待

from selenium import webdriver

browser = webdriver.Chrome()
browser.implicitly_wait(10)
browser.get(https://www.zhihu.com/explore)
input = browser.find_element_by_class_name(zu-top-add-question)
print(input)

显示等待

 1 from selenium import webdriver
 2 from selenium.webdriver.common.by import By
 3 from selenium.webdriver.support.ui import WebDriverWait
 4 from selenium.webdriver.support import expected_condition as EC
 5 
 6 
 7 browser = webdriver.Chrome()
 8 browser.get(https://www.taobao.com)
 9 
10 wait = WebDriverWait(browser,10)
11 input  = wait.until(EC.presence_of_element_located(()))
12 button = wait .until(EC.element_to_be_clickable(By_CSS_SELECTOR,btn-search))
13 print(input,button)

11.使用selenium模拟网络页面倒退与前进

 1 import time
 2 from  selenium import webdriver
 3 
 4 
 5 browser = webdriver.Chrome()
 6 browser.get(https://www.taobao.com)
 7 browser.get(https://www.baidu.com)
 8 browser.back()
 9 time.sleep()
10 browser.forwawrd()
11 browser.close()

12.使用选项卡进行操作设置

1 from selenium import webdriver
2 import time
3 
4 browser = webdriver.Chrome()
5 browser.get(https://www.baidu.com)
6 browser.execute_script(window.open())
7 browser.switch_to.window(browser.window.handles[1])
8 browser.get(https://www.zhihu.com)
9 browser.switch_to.window(browser.window.handle[0])

 

selenium的一些操作

标签:color   com   设置   pat   关闭   from   浏览器   second   until   

原文地址:https://www.cnblogs.com/LambertCao/p/11174833.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!