采用 1 个位置参数,但使用 Selenium Python 给出了 2 个错误

Takes 1 positional argument but 2 were given error using Selenium Python

您好,我想点击网站上的男性按钮:https://fs2.formsite.com/meherpavan/form2/index.html?1537702596407 但它给了我错误:

TypeError: element_to_be_clickable() takes 1 positional argument but 2 were given.

密码是

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


driver=webdriver.Chrome(executable_path="D:\ChromeDriverExtracted\chromedriver")
driver.get("https://fs2.formsite.com/meherpavan/form2/index.html?1537702596407")

WebDriverWait(driver, 2).until(EC.element_to_be_clickable(By.XPATH, "//type[@name='RESULT_RadioButton-7_0']")).click()

这是一个你应该传递到那里的元组,

EC.element_to_be_clickable((By.XPATH, "//type[@name='RESULT_RadioButton-7_0']")))

您需要在 element_to_be_clickable() 中传递一个 元组 ,如下所示:

EC.element_to_be_clickable((By.XPATH, "//type[@name='RESULT_RadioButton-7_0']"))

但是,您的工作代码行将是:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[@for='RESULT_RadioButton-7_0']"))).click()

浏览器快照:


此外,在 下面一行:

webdriver.Chrome(executable_path="D:\ChromeDriverExtracted\chromedriver") 

现在与 关联如下:

DeprecationWarning: executable_path has been deprecated, please pass in a Service object

因此您需要将 Service class 的实例作为参数传递,您的有效代码块将是:

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

s = Service('D:\ChromeDriverExtracted\chromedriver.exe')
driver = webdriver.Chrome(service=s)
driver.get("https://fs2.formsite.com/meherpavan/form2/index.html?1537702596407")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[@for='RESULT_RadioButton-7_0']"))).click()

参考资料

您可以在以下位置找到一些相关的详细讨论:

  • Use Selenium with Brave Browser pass service object written in python