TypeError: element_to_be_clickable() takes 1 positional argument but 2 were given運行以下代碼時出現錯誤:
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
url = 'https://www.expedia.co.uk/'
s = Service(ChromeDriverManager().install())
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
driver = webdriver.Chrome(service=s, options=chrome_options)
driver.get(url)
elem = WebDriverWait(driver, 10).until(EC.element_to_be_clickable(By.CSS_SELECTOR, "#add-flight-switch"))
elem.click()
elem1 = driver.find_element(By.XPATH, "button[aria-label='Leaving from']").text
driver.quit()
我可以看到elements_to_be_clickable()函式中確實有兩個引數,但是By.CSS_SELECTOR不需要通過特定的選擇器型別(在本例中為 CSS)找到的部分嗎??
我正在使用選擇器集線器來獲取 CSS_SELECTOR 資訊。
非常感謝有關如何解決的任何支持。
uj5u.com熱心網友回復:
您確實需要兩者By和值,但該函式希望兩個值都打包為一個,在一個tuple
EC.element_to_be_clickable((By.CSS_SELECTOR, "#add-flight-switch"))
如果您深入查看源代碼,您將看到內部element_to_be_clickable使用_find_element,此函式解壓縮tuple要使用的driver.find_element()
class element_to_be_clickable(object):
def __init__(self, locator):
self.locator = locator
def __call__(self, driver):
element = visibility_of_element_located(self.locator)(driver)
...
class visibility_of_element_located(object):
def __init__(self, locator):
self.locator = locator
def __call__(self, driver):
return _element_if_visible(_find_element(driver, self.locator))
...
def _find_element(driver, by):
...
return driver.find_element(*by)
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/349829.html
上一篇:Python請求表單資料方法
