Selenium 库的基本用法

Posted zll20153246

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Selenium 库的基本用法相关的知识,希望对你有一定的参考价值。

  1.基本使用

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

brower = webdriver.Chrome()
try:
    brower.get(https://www.baidu.com)
    input = brower.find_element_by_id(kw)#查找id为kw的元素
    input.send_keys(Python)#向input文本框输入python
    input.send_keys(Keys.ENTER)#输入回车
    wait = WebDriverWait(brower,10)#等待浏览器加载
    wait.until(EC.presence_of_element_located((By.ID,content_left)))#查找id为content——left的对象
    print(brower.current_url)#输出当前的url
    print(brower.get_cookies())#输出当前的cookies
    print(brower.page_source)#输出当前的网页源码
finally:
    brower.close()

  2.声明浏览器对象

from selenium import webdriver

brower = webdriver.Chrome()
brower = webdriver.Firefox()
brower = webdriver.Edge()
brower = webdriver.PhanromJS()
brower = webdriver.Safari()

  3.访问页面

from selenium import webdriver
brower = webdriver.Chrome()
brower.get(https://ww.taobao.com)
print(brower.page_source)
brower.colse()

  4.查找元素

  单个元素

from selenium import webdriver
browser = webdriver.Chrome()
browser.get(https://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,input_second,input_third)
browser.close()

结果:

<selenium.webdriver.remote.webelement.WebElement (session="aafcd8308ef345a4014a0db417267640", element="0.8704970628206692-1")> <selenium.webdriver.remote.webelement.WebElement (session="aafcd8308ef345a4014a0db417267640", element="0.8704970628206692-1")> <selenium.webdriver.remote.webelement.WebElement (session="aafcd8308ef345a4014a0db417267640", element="0.8704970628206692-1")>

from selenium import webdriver from selenium.webdriver.common.by 
import By browser = webdriver.Chrome()
browser.get(
https://www.taobao.com)
input_first
=browser.find_element(By.ID,q)
print(input_first) browser.close()

结果:
<selenium.webdriver.remote.webelement.WebElement (session="ad3663f850d6c3fd81751c5c8cf45511", element="0.6365953152365587-1")>
 

  多个元素

from selenium import webdriver
browser = webdriver.Chrome()
browser.get(https://www.taobao.com)
list=browser.find_elements_by_css_selector(.service-bd li)
print(list)
browser.close()


from selenium import webdriver
browser = webdriver.Chrome()
browser.get(https://www.taobao.com)
list=browser.find_elements(By.CSS_SELECTOR,.service-bd li)
print(list)
browser.close()

  5.元素交互操作

from selenium import webdriver
import time
browser = webdriver.Chrome()
browser.get(https://www.taobao.com)
input=browser.find_element_by_id(q)
input.send_keys(iPhone)
time.sleep(1)
input.clear()
input.send_keys(iPad)
button = browser.find_element_by_class_name(btn-search)
button.click()

  6.交互动作

from selenium import webdriver
from selenium.webdriver import ActionChains
brower=webdriver.Chrome()
url="https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable"
brower.get(url)
brower.switch_to.frame(iframeResult)
source=brower.find_element_by_css_selector(#draggable)
target=brower.find_element_by_css_selector(#droppable)
action=ActionChains(brower)
action.drag_and_drop(source,target)
action.perform()

  7.执行javascript

from selenium import webdriver
brower = webdriver.Chrome()
brower.get(https://www.zhihu.com/explore)
brower.execute_script(window.scrollTo(0,document.body.scrollHeight))
brower.execute_script(alert("To Bottom"))

  8.获取属性

from selenium import webdriver
from selenium.webdriver import ActionChains
brower = webdriver.Chrome()
url=https://www.zhihu.com/explore
brower.get(url)
logo = brower.find_element_by_id(zh-top-link-logo)
print(logo)
print(logo.get_attribute(class))

获取:
<selenium.webdriver.remote.webelement.WebElement (session="5578b6070e5cd404d5636c6c15d1062e", element="0.2337143497044789-1")>
zu-top-link-logo

  9.获取文本值

from selenium import webdriver
brower = webdriver.Chrome()
url=https://www.zhihu.com/explore
brower.get(url)
input=brower.find_element_by_class_name(zu-top-add-question)
print(input.text)

结果:
提问

  10.获取ID,位置,标签名,大小

from selenium import webdriver
brower = webdriver.Chrome()
url=https://www.zhihu.com/explore
brower.get(url)
input=brower.find_element_by_class_name(zu-top-add-question)
print(input.id)
print(input.location)
print(input.tag_name)
print(input.size)

结果:
0.5790966028649505-1
{‘x‘: 760, ‘y‘: 7}
button
{‘height‘: 32, ‘width‘: 66}

  11.Frame

import time
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
brower = webdriver.Chrome()
url="https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable"
brower.get(url)
brower.switch_to.frame(iframeResult)
source=brower.find_element_by_css_selector(#draggable)
print(source)
try:
    logo=brower.find_element_by_class_name(logo)
except NoSuchElementException:
    print(No Logo)
brower.switch_to.parent_frame()
logo=brower.find_element_by_class_name(logo)
print(logo)
print(logo.text)


结果:
<selenium.webdriver.remote.webelement.WebElement (session="4515a548a628f32ec7ebb86b845cafbc", element="0.47138378750440957-1")>
No Logo
<selenium.webdriver.remote.webelement.WebElement (session="4515a548a628f32ec7ebb86b845cafbc", element="0.590628320406164-2")>
RUNOOB.COM

  12.隐式等待

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

结果:
<selenium.webdriver.remote.webelement.WebElement (session="df8f8bebb2cf5356c12442f1bcc5c2f7", element="0.40275892523916035-1")>

  13.显示等待

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
brower = webdriver.Chrome()
brower.get(https://www.taobao.com)
wait=WebDriverWait(brower,10)
input=wait.until(EC.presence_of_element_located((By.ID,q)))
button=wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,.btn-search)))
print(input,button)

结果:
<selenium.webdriver.remote.webelement.WebElement (session="036c1bc8fad6ee8e9ee42266ce163be7", element="0.8588910449888032-1")> <selenium.webdriver.remote.webelement.WebElement (session="036c1bc8fad6ee8e9ee42266ce163be7", element="0.8588910449888032-2")>

  14.前进后退

from selenium import webdriver
import time
brower = webdriver.Chrome()
brower.get(https://www.baidu.com)
brower.get(https://www.taobao.com)
brower.get(https://www.python.org)
brower.back()
time.sleep(1)
brower.forward()
brower.close()

  15.Cookies

from selenium import webdriver
brower = webdriver.Chrome()
brower.get(https://www.zhihu.com/explore)
print(brower.get_cookies())
brower.add_cookie({name:name,domain:www.zhihu.com,value:germey})
print(brower.get_cookies())
brower.delete_all_cookies()
print(brower.get_cookies())

  16.选项卡管理

import time
from selenium import webdriver

brower = webdriver.Chrome()
brower.get(https://www.baidu.com)
brower.execute_script(window.open())
print(brower.window_handles)
brower.switch_to_window(brower.window_handles[1])
brower.get(https://www.taobao.com)
time.sleep(1)
brower.switch_to_window(brower.window_handles[0])
brower.get(https://www.python.org)

结果:
[‘CDwindow-4971CB8E7903DFAE9CBA82A6A5F05DB2‘, ‘CDwindow-652D8829B41E67E7F7E8C2E146F8C353‘]

  17.异常处理

from selenium import webdriver
brower=webdriver.Chrome()
brower.get(https://www.baidu.com)
brower.find_element_by_id(hello)
from selenium import webdriver
from selenium.common.exceptions import TimeoutException,NoSuchElementException
brower=webdriver.Chrome()
try:
    brower.get(https://www.baidu.com)
except TimeoutException:
    print(Time Out)
try:
    brower.find_element_by_id(hello)
except NoSuchElementException:
    print(No Element)
finally:
    brower.close()

结果:
No Element

 


















以上是关于Selenium 库的基本用法的主要内容,如果未能解决你的问题,请参考以下文章

Selenium+python自动化12+日志logging基本用法高级用法

python3爬虫

知名一线大咖带你简单学习Selenium的基本用法

selenium模块的基本使用

windows下boost库的基本用法

RF第二讲--Selenium2Library库的简单实用