selenium: wait for element with AND / OR

Is this OK?

CSS waiter

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By


class ByCss(object):
    def __init__(self, *args, **kwargs):
        self.meet_all = kwargs.get('meet_all', False)
        self.ecs = [
            EC.presence_of_element_located((By.CSS_SELECTOR, i)) for i in args]

    def __call__(self, driver):
        if self.meet_all:
            return all([fn(driver) for fn in self.ecs])
        else:
            for fn in self.ecs:
                try:
                    if fn(driver): return True
                except:
                    pass

    @classmethod
    def wait(cls, driver, *args, **kwargs):
        seconds = kwargs.get('seconds', 10)
        WebDriverWait(driver, seconds).until(cls(*args, **kwargs))
        return [driver.find_elements_by_css_selector(i) for i in args]

sample

from selenium import webdriver
br = webdriver.Safari()
login_portal(br)
def login_portal(br):
    usr_name_css = 'form[name="auth"] input[name="usr_name"]'
    usr_password_css = 'form[name="auth"] input[name="usr_password"]'

    usr_name, usr_password = ByCss.wait(
        br, usr_name_css, usr_password_css, meet_all=True)

    usr_name[0].send_keys(USER)
    usr_password[0].send_keys(PASSWORD)

    submit_css = 'form[name=auth] input[type=submit]'
    br.find_elements_by_css_selector(submit_css)[0].click()

Recommended Posts

selenium: wait for element with AND / OR
Scraping with Python, Selenium and Chromedriver
Get html from element with Python selenium
Practice web scraping with Python and Selenium
Plot black and white graphs suitable for papers with matplotlib or pylab
Scraping with selenium
Scraping with selenium ~ 2 ~
Wait for another window to open in Selenium
Causal reasoning and causal search with Python (for beginners)
Try running Google Chrome with Python and Selenium
phantomjs and selenium
Install pip and pandas with Ubuntu or VScode
Scraping with Selenium
Beginning with Selenium
Drag and drop local files with Selenium (Python)
Install selenium on Mac and try it with python
Automatic follow on Twitter with python and selenium! (RPA)
Automate Chrome with Python and Selenium on your Chromebook
Firefox add-on that copies element specifications for Selenium WebDriver
I tried a simple RPA for login with selenium
Automatically translate DeepL into English with Python and Selenium
Install tweepy with pip and use it for API 1.1
Template for Bottle when deploying with Github and Bitbucket
Successful scraping with Selenium
ScreenShot with Selenium (Python)
Seeking a unified way to wait and get for state changes in Selenium for Python elements
Scraping with Selenium [Python]
3D or D with Py
With and without WSGI
Installation procedure for Python and Ansible with a specific version
Analyze stocks with python and look for favorable trading phases
Comfortable LaTeX with Windows Subsystem for Linux and VS Code
4 Techniques for Creating Diagrams for Papers and Presentation Materials with matplotlib
Library for specifying a name server and dig with python
This and that for using Step Functions with CDK + Python
Performance comparison between 2D matrix calculation and for with numpy
Create AND / OR / NAND / XOR circuits with FPGA that may be necessary for deep learning with Polyphony