爬虫- selenium使⽤

1. 定位元素

**find_element_by_id:根据id来查找某个元素

1 submitTag = driver.find_element_by_id('su') 
2 submitTag1 = driver.find_element(By.ID,'su') 

find_element_by_class_name:根据类名查找元素

1 submitTag = driver.find_element_by_class_name('su') 
2 submitTag1 = driver.find_element(By.CLASS_NAME,'su') 

find_element_by_name:根据name属性的值来查找元素

1 submitTag = driver.find_element_by_name('email')
2 submitTag1 = driver.find_element(By.NAME,'email') 

find_element_by_tag_name:根据标签名来查找元素

1 submitTag = driver.find_element_by_tag_name('div') 
2 submitTag1 = driver.find_element(By.TAG_NAME,'div')

find_element_by_xpath:根据xpath语法来获取元素

1 submitTag = driver.find_element_by_xpath('//div') 
2 submitTag1 = driver.find_element(By.XPATH,'//div')

find_element_by_css_selector:根据css选择器选择元素


1 submitTag = driver.find_element_by_css_selector('//div') 
2 submitTag1 = driver.find_element(By.CSS_SELECTOR,'//div') 

注意: find_element 是获取第⼀个满⾜条件的元素。 find_elements 是获取所有满⾜条件的元素

from selenium import webdriver
import time
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
# 打开百度
driver.get('https://www.baidu.com/')
# 定位元素
 #find_element_by_id 根据id来查找某个元素
driver.find_element_by_id('kw').send_keys('python')
driver.find_element(By.ID,'kw').send_keys('java')
# find_element_by_class_name:根据类名查找元素
driver.find_element_by_class_name('s_ipt').send_keys('apple')
driver.find_element(By.CLASS_NAME,'s_ipt').send_keys('egg')
#find_element_by_name:根据name属性的值来查找元素
driver.find_element_by_name('wd').send_keys('bag')
driver.find_element(By.NAME,'wd').send_keys('666')
# • find_element_by_tag_name:根据标签名来查找元素
head = driver.find_element_by_tag_name('head')
# print(head)
# find_element_by_xpath:根据xpath语法来获取元素
driver.find_element_by_xpath('//input[@id="kw"]').send_keys('同学们')
driver.find_element_by_xpath('//*[@id="kw"]').send_keys('同学们')
# find_element_by_css_selector:根据css选择器选择元素
# . class # id
driver.find_element_by_css_selector('.s_ipt').send_keys('python')
inputTag = driver.find_elements_by_tag_name('input')
print(inputTag,len(inputTag))
# time.sleep(2)
# driver.close()
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
# 打开百度
driver.get('https://www.baidu.com/')
# 窗口最大化
driver.maximize_window()
#driver.minimize_window()  关注源码
# 等待3秒
time.sleep(3)
# 关闭窗口
driver.close()
# 退出浏览器
driver.quit()

2. 操作表单元素

操作输⼊框:分为两步。
第⼀步:找到这个元素。
第⼆步:使⽤send_keys(value),将数据填充进去 使⽤clear⽅法可以清除输⼊框中的内容 。

inputTag.clear() 

操作checkbox
因为要选中checkbox标签,在⽹⻚中是通过⿏标点击的。因此想要选中 checkbox标签,那么先选中这个标签,然后执⾏click事件。

rememberTag = driver.find_element_by_name("rememberMe")  rememberTag.click() 

操作按钮
操作按钮有很多种⽅式。⽐如单击、右击、双击等。这⾥讲⼀个最常⽤的,就 是点击。直接调⽤click函数就可以了。


inputTag = driver.find_element_by_id('su') 
inputTag.click() 

选择select
select元素不能直接点击。因为点击后还需要选中元素。这时候selenium就专 ⻔为select标签提供了⼀个类selenium.webdriver.support.ui.Select。将获取 到的元素当成参数传到这个类中,创建这个对象。以后就可以使⽤这个对象进⾏选择了。https://www.17sucai.com/boards/53562.html 。

3. ⾏为链

有时候在⻚⾯中的操作可能要有很多步,那么这时候可以使⽤⿏标⾏为链类 ActionChains来完成。⽐如现在要将⿏标移动到某个元素上并执⾏点击事件。

actions = ActionChains(driver)
actions.move_to_element(inputTag)
actions.send_keys_to_element(inputTag,'python') actions.move_to_element(submitTag)
actions.context_click() 
actions.click(submitTag)
actions.perform() 

还有更多的⿏标相关的操作:
click_and_hold(element):点击但不松开⿏标。
context_click(element):右键点击。
double_click(element):双击。

from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
driver=webdriver.Chrome()
driver.get('https://www.baidu.com/')
input_tag=driver.find_element_by_id('kw')
submitBtn=driver.find_element_by_id('su')
actions=ActionChains(driver)# 实例化
# 把鼠标移动到输入框
actions.move_to_element(input_tag)
actions.send_keys_to_element(input_tag,'python')# 输入内容
actions.move_to_element(submitBtn)# 点击按钮 百度一下
actions.click()
actions.context_click()# 点击右键
actions.perform()# 提交行为链上的操作

更多⽅法请参考:http://selenium-python.readthedocs.io/api.html。

4. Selenium⻚⾯等待

4.1 Cookie操作

获取所有的cookie

cookies = driver.get_cookies()

根据cookie的name获取cookie

value = driver.get_cookie(key) 

删除某个cookie

driver.delete_cookie('key') 
4.2 ⻚⾯等待

现在的⽹⻚越来越多采⽤了 Ajax 技术,这样程序便不能确定何时某个元素完全加载出来了。如果实际⻚⾯等待时间过⻓导致某个dom元素还没出来,但是你的代码直接使⽤了这个WebElement,那么就会抛出NullPointer的异常。为了 解决这个问题。 Selenium 提供了两种等待⽅式:⼀种是隐式等待、⼀种 是显式等待 。
隐式等待:调⽤driver.implicitly_wait。那么在获取不可⽤的元素之前,会 先等待10秒中的时间 。

driver.implicitly_wait(10) 

显示等待:显示等待是表明某个条件成⽴后才执⾏获取元素的操作。也可以 在等待的时候指定⼀个最⼤的时间,如果超过这个时间那么就抛出⼀个异常。显示等待应该使⽤selenium.webdriver.support.excepted_conditions 期望的条件和selenium.webdriver.support.ui.WebDriverWait来配合完成 。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")
try: 
  element = WebDriverWait(driver, 10).until(
   EC.presence_of_element_located((By.ID, "myDynamicElement")) 
   ) 
finally: 
     driver.quit() 

⼀些其他的等待条件
presence_of_element_located:某个元素已经加载完毕了。 presence_of_all_elements_located:⽹⻚中所有满⾜条件的元素都加载完 毕了。
element_to_be_clickable:某个元素是可以点击了。
更多条件请参考:http://selenium-python.readthedocs.io/waits.html 。

5. 打开多窗⼝和切换⻚⾯

有时候窗⼝中有很多⼦tab⻚⾯。这时候肯定是需要进⾏切换的。selenium 提供了⼀个叫做switch_to_window来进⾏切换,具体切换到哪个⻚⾯,可以 从driver.window_handles中找到。

# 打开⼀个新的⻚⾯ 
driver.execute_script("window.open('url')")
print(driver.current_url) 
# 切换到这个新的⻚⾯中 
driver.switch_to_window(self.driver.window_handles[1]) 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值