Python Selenium.WebDriverWait determines whether an element exists

Posted by csueiras on Sat, 25 Dec 2021 08:21:32 +0100

Python Selenium.WebDriverWait determines whether an element exists

1, Determine whether the element exists

Selenium does not have a direct method to judge whether an element exists, so we can only write a special function or method through some existing methods. Here I summarize three methods for your reference, which can be selected according to their advantages and disadvantages and application scenarios

As for the function of judging whether elements exist, I think there are mainly two aspects:

  • First, judge whether the element exists before operating it, which can effectively avoid error reporting
  • Another is to judge whether the element exists, and then execute some code logic only if it exists, such as operating on other elements in the page

In the following example, I will use an xpath expression or id to capture an element

2, Exception capture

Exception capture is the first method that can be thought of. If the element does not exist when obtaining the element, a NoSuchElementException exception that does not find the element will be thrown.
Before you start, you need to know that Selenium places all exceptions in Selenium common. Under exceptions, you need to import the file from Selenium common import exceptions

def iselement(browser, xpaths):
    """
    The basic implementation determines whether the element exists
    :param browser: Browser object
    :param xpaths: xpaths expression
    :return: Does it exist
    """
    try:
        browser.find_element_by_xpath(xpaths)
        return True
    except exceptions.NoSuchElementException:
        return False

If we don't just judge whether the element exists, we also need to process the element when it exists. According to the above code, we need to execute the capture behavior twice, the first is to judge and the second is to operate on the element. We can optimize it so that it can be captured at one time.

If you just want to get the text in the element, you can write it this way. You only need to improve it on the original basis

def iselement(browser, xpaths, istest=False):
    """
    The implementation determines whether the element exists
    :param browser: Browser object
    :param xpaths: xpaths expression
    :param istest: If yes True,If the element exists, the returned content will be the element text content
    :return: Does it exist
    """
    try:
        target = browser.find_element_by_xpath(xpaths)
    except exceptions.NoSuchElementException:
        return False
    else:
        if istest:
            return target.text
        return True

If you need to perform other operations, you can directly return the element object

def get_element(browser, xpaths):
    """
    Determine whether an element exists and get the element object
    :param browser: Browser object
    :param xpaths: xpaths expression
    :return: Element object or empty
    """
    try:
        target = browser.find_element_by_xpath(xpaths)
    except exceptions.NoSuchElementException:
        return
    else:
        return target

For exceptions that show waiting, it is exceptions TimeoutException

3, Multi element capture null judgment

Selenium also has methods for finding multiple elements at once, which return a list

Single elementMultiple elementsdescribe
find_element_by_idnothingFind element by ID
find_element_by_namefind_elements_by_nameFind element by Name
find_element_by_xpathfind_elements_by_xpathFind elements through XPath
find_element_by_link_textfind_elements_by_link_textGet hyperlink from link text
find_element_by_partial_link_textfind_elements_by_partial_link_textGet hyperlink from link text
find_element_by_tag_namefind_elements_by_tag_nameFind element by tag name
find_element_by_class_namefind_elements_by_class_nameLocate the element by Class name
find_element_by_css_selectorfind_elements_by_css_selectorFind elements through CSS selectors

For elements that cannot be obtained, an empty list will be returned. According to the principle of whether it is empty, you can judge whether the element exists

def iselement(browser, xpaths, istest=False):
    """
    The implementation determines whether the element exists
    :param browser: Browser object
    :param xpaths: xpaths expression
    :param istest: If yes True,If the element exists, the returned content will be the element text content
    :return: Does it exist
    """
    target = browser.find_elements_by_xpath(xpaths)
    if not target:
        return False
    if istest:
        return target[0].text
    return True
def get_element(browser, xpaths):
    """
    Determine whether an element exists and get the element object
    :param browser: Browser object
    :param xpaths: xpaths expression
    :return: Element object or empty
    """
    target = browser.find_elements_by_xpath(xpaths)
    if target:    
        return target[0]

4, Page source code acquisition

This method is not very practical, but it can be considered for pages whose target content has been loaded
The principle is very simple, that is to pass the source code of the current page through browser page_ The source attribute is obtained, and then the source code is submitted to these libraries for processing, further data judgment and extraction

from lxml import etree
from bs4 import BeautifulSoup

Page parsing using BeautifulSoup

soup = BeautifulSoup(browser.page_source, "html.parser")

Page parsing using etree

tree = etree.HTML(browser.page_source)

reference material 💌

Heartfelt thanks 💖

Related blog 😏

Topics: Python Selenium WebDriver