当前位置:网站首页>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 .

*
原网站

版权声明
本文为[There is nothing else in Qing'an]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111535135271.html