ich will gerade eine klasse erstellen, mit der ich einen url regex-match in selenium überprüfen kann.
ich habe nun das hier gefunden:
Code: Alles auswählen
import re
from selenium.webdriver.support.expected_conditions import _find_element
class text_match(object):
def __init__(self, locator, regexp):
self.locator = locator
self.regexp = regexp
def __call__(self, driver):
element_text = _find_element(driver, self.locator).text
return re.search(self.regexp, element_text)
Usage:
WebDriverWait(driver, 60).until(
text_match((By.XPATH, "//tr[5]/td[11]/div"), r"[0,1]{1}.[0-9]{6}")
)
Code: Alles auswählen
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import re
class url_match(object):
def __init__(self, regexp):
self.regexp = regexp
def __call__(self, driver):
urlmatch_re = EC.url_matches(driver, self.locator).text
return re.search(self.regexp, urlmatch_re)
und so angewendet:
element = WebDriverWait(driver, 60).until(
url_match((driver.current_url), r"https\:\/\/REGEX_URL")
Und die url_matches funktion von selenium sieht so aus:
Code: Alles auswählen
class url_matches(object):
"""An expectation for checking the current url.
pattern is the expected pattern, which must be an exact match
returns True if the title matches, false otherwise."""
def __init__(self, pattern):
self.pattern = pattern
def __call__(self, driver):
import re
match = re.search(self.pattern, driver.current_url)
return match is not None
Aber ich bekomme nun den Fehler:
TypeError: __init__() takes 2 positional arguments but 3 were given
Was mache Ich bei der Definition der Klasse falsch?