python selenium的使用

*隐式等待

  driver.implicitly_wait(time_to_wait)

  当查找节点而节点并没有立即出现的时候,隐形等待一段时间直到元素被找到或命令完成,该命令在每个会话中仅需执行一次

  缺点: 如果没有定位到元素,游览器也会等待设置的时间再进行下一步操作

In [5]: driver = webdriver.Chrome()

DevTools listening on ws://127.0.0.1:12823/devtools/browser/5e6b75be-748f-42e0-a726-02883e6ff2ef

In [6]: ?driver.implicitly_wait
Signature: driver.implicitly_wait(time_to_wait)
Docstring:
Sets a sticky timeout to implicitly wait for an element to be found,
   or a command to complete. This method only needs to be called one
   time per session. To set the timeout for calls to
   execute_async_script, see set_script_timeout.

:Args:
 - time_to_wait: Amount of time to wait (in seconds)

:Usage:
    driver.implicitly_wait(30)

 

*显式等待

先指定好某个要查找的节点,然后指定一个最长的等待时间,默认每0.5秒轮询一次,如果在规定时间内加载出来了这个节点,那就返回查找的节点,如果到了规定时间依然没有加载出该节点,则会抛出超时异常。

且在调用期间默认忽略NoSuchElementException异常,注解如下:

In [7]: from selenium.webdriver.support.ui import WebDriverWait

In [8]: ?WebDriverWait
Init signature: WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)
Docstring:      <no docstring>
Init docstring:
Constructor, takes a WebDriver instance and timeout in seconds.

:Args:
 - driver - Instance of WebDriver (Ie, Firefox, Chrome or Remote)
 - timeout - Number of seconds before timing out
 - poll_frequency - sleep interval between calls
   By default, it is 0.5 second.
 - ignored_exceptions - iterable structure of exception classes ignored during calls.
   By default, it contains NoSuchElementException only.

Example:
 from selenium.webdriver.support.ui import WebDriverWait

 element = WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id("someId"))

 is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException)).\

             until_not(lambda x: x.find_element_by_id("someId").is_displayed())
File:           d:\anaconda3\lib\site-packages\selenium\webdriver\support\wait.py
Type:           type

 

*until方法,传入等待条件传入要等待条件 expected_conditions

In [12]: ?WebDriverWait.until
Signature: WebDriverWait.until(self, method, message='')
Docstring: Calls the method provided with the driver as an argument until the         return value is not False.
File:      d:\anaconda3\lib\site-packages\selenium\webdriver\support\wait.py
Type:      function

 

*expected_conditions举例

from selenium.webdriver.support import expected_conditions as EC #引入
base_url = "http://www.baidu.com"
wait = WebDriverWait(driver,10)

In [23]:wait.until(EC.title_is(u"百度一下,你就知道")) #用来判断title,返回Boolean

Out[23]: True

In [33]: wait.until(EC.title_contains(u"百度一下"))#判断title中是否包含字符,返回Boolean
Out[33]: True

In [46]: locator = ('xpath', "//*[@id='u1']/a[8]")

In [49]: wait.until(EC.text_to_be_present_in_element(locator,u"设置")) #判断指定的元素中是否包含了预期的字符串,返回Boolean

Out[49]: True

 

#presence_of_element_located 节点加载出,传入定位元组,如(By.ID, 'p')

#presence_of_all_elements_located 所有节点加载出

#element_to_be_clickable 节点可点击

#alert_is_present是否出现Alert

 

例:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(chrome_options=option)
wait = WebDriverWait(driver, 5)
datarange = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="managed-accounts-view"]/div/div[1]/div/date-range-selector/div/div[1]/*'))) #节点可点击
datarange.click()

 

转载于:https://www.cnblogs.com/zxf0/p/9711529.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值