当前位置:网站首页>Selenium-- display waiting (medium) -- detailed explanation
Selenium-- display waiting (medium) -- detailed explanation
2022-06-11 15:47:00 【There is nothing else in Qing'an】
Preface
This is Qing'an , The previous chapter discussed the theory of display waiting and some usage , In this chapter, we will talk about Expected_conditions It's usually called EC modular .
*Be careful : Regardless of 3.0 or 4.0 Of , All have this module . This chapter uses 4.2 Of .
*
This chapter mainly explains in the form of packaging . Get on the !
Rewinding
from selenium.webdriver.support import expected_conditions as EC
title_is
Check the expectations of the page title .title Is the expected title , Must match exactly if the title matches , Then return to True, Otherwise return to false.
# ---- Qing'an —---
# WeChat :qing_an_an
# official account : Measured number der
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
class Brouser:
fox = webdriver.Firefox()
wait = WebDriverWait(fox, 5)
def get_url(self,url):
self.fox.get(url)
def title(self,value):
self.wait.until(EC.title_is(value),message=' The title doesn't match ')
if __name__ == '__main__':
b = Brouser()
b.get_url('http://shop.aircheng.com/simple/login')
b.title(' The user login - iWebShop Mall demo ')
「 Do an analysis of the above code , The following codes are used 」
The writing here may be different from what you see on the Internet , But it's not a big problem , How to write depends on yourself . Mainly the writing of methods Here we will directly drive 、 Show that the wait is written as an attribute in the class , Defined a url Method , Used to open the browser address .
title_is Determine the title of the browser , If the title is wrong , Will output message A reminder of , for example :
selenium.common.exceptions.TimeoutException: Message: The title doesn't match
Finally, another execution entry , It instantiates the class and invokes various methods in the class . Value transfer is required , Unwanted direct calls
title_contains
Check whether the title contains case sensitive substrings .title Is the desired title fragment. When the title matches, it returns True, Otherwise return to False
# ---- Qing'an —---
# WeChat :qing_an_an
# official account : Measured number der
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
class Brouser:
fox = webdriver.Firefox()
wait = WebDriverWait(fox, 5)
def get_url(self,url):
self.fox.get(url)
def contains_title(self,value):
self.wait.until(EC.title_contains(value))
if __name__ == '__main__':
b = Brouser()
b.get_url('http://shop.aircheng.com/simple/login')
b.contains_title(' The user login - iWebShop Mall demo ')
b.contains_title('iWebShop Mall demo ')
This one , With a few , It can also be used to determine whether the specified 、 Specific text , As shown above .
presence_of_element_located
Check DOM Whether there is an element on the expected page . This does not necessarily mean that the element is visible . positioner - Used to find elements WebElement Then return to the WebElement
# ---- Qing'an —---
# WeChat :qing_an_an
# official account : Measured number der
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver
class Brouser:
fox = webdriver.Firefox()
wait = WebDriverWait(fox, 5)
def get_url(self,url):
self.fox.get(url)
def presence_located(self,value,*ele):
el = self.wait.until(EC.presence_of_element_located(ele),message=' Expected element not found ')
el.send_keys(value)
if __name__ == '__main__':
b = Brouser()
b.get_url('http://shop.aircheng.com/simple/login')
b.presence_located('qingan',By.NAME,'login_info')
「 The positioner mentioned above - Used to find elements WebElement Then return to the WebElement, So we are presence_of_element_located(ele) After this element is passed in , After displaying and waiting for judgment, we know that the element exists in the interface , Then return this element , We can directly take this value for the next operation , For example, the above input operation .」
Besides , The above also said This does not necessarily mean that the element is visible , It means that invisible elements can also be detected , But you can't do anything else .
visibility_of_element_located
Check whether the element exists on the page and is visible . Visibility means that not only are elements displayed, but their height and width are also greater than 0. positioner - Used to find elements found and visible WebElement Then return to the WebElement
# ---- Qing'an —---
# WeChat :qing_an_an
# official account : Measured number der
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver
class Brouser:
fox = webdriver.Firefox()
wait = WebDriverWait(fox, 5)
def get_url(self,url):
self.fox.get(url)
def visibility_located(self,value,*ele):
el = self.wait.until(EC.visibility_of_all_elements_located(ele), message=' Expected element not found ')
el[0].send_keys(value)
if __name__ == '__main__':
b = Brouser()
b.get_url('http://shop.aircheng.com/simple/login')
b.visibility_located('qingan',By.NAME,'password')
What is worth saying here is el[0], Why el[0], Because the way of writing here returns to a list list , Therefore, you need to get the elements by taking values .
url_to_be
Check current url The expectation of .url It's expected url, Must match exactly . If url matching , Then return to True, Otherwise return to false
# ---- Qing'an —---
# WeChat :qing_an_an
# official account : Measured number der
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver
class Brouser:
fox = webdriver.Firefox()
wait = WebDriverWait(fox, 5)
def get_url(self,url):
self.fox.get(url)
def url_be(self,url):
self.wait.until(EC.url_to_be(url))
if __name__ == '__main__':
b = Brouser()
b.get_url('http://shop.aircheng.com/simple/login')
b.url_be('http://shop.aircheng.com/simple/login')
Judge url There are several other ways , It's all the same , You can see .
*「url_matches」 : Check current url The expectation of .pattern Is the expected pattern , Must be an exact match if url matching , Then return to True, Otherwise return to false. 「url_contains」 : Check current url Whether to include case sensitive substrings .url Yes required url Fragments of ,url Return... When matching True, Otherwise return to False
*
「url_changes」 : Check current url The expectation of .url It's expected url, Cannot match exactly if url Different , Then return to True, Otherwise return to false.
visibility_of
Check for... That are known to exist on the page DOM Is visible . Visibility means that an element is more than just a display , But the height and width are also greater than 0. The element is WebElement stay WebElement Returns... When visible ( same )WebElement
# ---- Qing'an —---
# WeChat :qing_an_an
# official account : Measured number der
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver
class Brouser:
fox = webdriver.Firefox()
wait = WebDriverWait(fox, 5)
def get_url(self,url):
self.fox.get(url)
def visibility_(self,*ele):
el = self.wait.until(EC.visibility_of(self.fox.find_element(*ele)))
el.click()
if __name__ == '__main__':
b = Brouser()
b.get_url('http://shop.aircheng.com/simple/login')
b.visibility_(By.NAME, 'remember')
Here's what's interesting ,visibility_of_element_located Follow visibility_of Is very similar . It is different from the above EC.visibility_of What I write in it is positioning , Not just elements . This is also a difference . Pay attention to the following use .
*Last ! The way , The above clearly mentioned True Follow Flase But I didn't read the article .
*
One 、 You can give a variable, such as... Above el, direct print(el) You can see
Two 、 Because for True You can directly move down to other steps , There is no need to see True. If it is False It will tell you that you are wrong .
边栏推荐
- Introduction and use of etcd
- 验证码是自动化的天敌?阿里研究出了解决方法
- Cf662b graph coloring problem solution
- The third generation Pentium B70 won the C-NCAP five-star safety performance again
- In June, 2019, cat teacher's report on monitoring
- openGauss数据库闪回功能验证
- [daily question series]: how to test web forms?
- Hands on, how should selenium deal with pseudo elements?
- 英伟达终于开源GPU内核模块代码,网友:难以置信
- Go language - value types and reference types
猜你喜欢

【0006】title、关键字及页面描述

Introduction to JVM basic concepts

Arthas practice documentation

05 _ In simple terms index (Part 2)

07 _ 行锁功过:怎么减少行锁对性能的影响?

【创建型模式】工厂方法模式
![[creation mode] single instance mode](/img/80/b90c7358de9670e9b07d28752efee5.png)
[creation mode] single instance mode

Idea2021.1 installation tutorial

Maui introductory tutorial series (1. framework introduction)

验证码是自动化的天敌?阿里研究出了解决方法
随机推荐
【创建型模式】建造者模式
从内核代码了解SQL如何解析
Introduction to JVM basic concepts
Don't you understand the design and principle of thread pool? Break it up and crush it. I'll teach you how to design the thread pool
The difference between go language array and slice
GO语言-Slice切片
前沿科技探究DeepSQL:库内AI算法
让快递快到来不及退款的,真的不是人
openGauss数据库ODBC环境连接配置(Windows)
openGauss 多线程架构启动过程详解
码农必备SQL调优(上)
Daily blog - wechat service permission 12 matters
英伟达终于开源GPU内核模块代码,网友:难以置信
数据库密态等值查询概述及操作
Zero foundation self-study software test, I spent 7 days sorting out a set of learning routes, hoping to help you
File is in use and cannot be renamed solution
微软韦青:狗尾巴的故事—数智时代的第一性原理 | 极客时间
Devil cold rice # 037 devil shares the ways to become a big enterprise; Female anchor reward routine; Self discipline means freedom; Interpretation of simple interest and compound interest
【创建型模式】单例模式
Kaixia takes the lead in launching a new generation of UFS embedded flash memory devices that support Mipi m-phy v5.0