用Python实现WebDriverWait时出现问题-InvalidArgumentException

用Python实现WebDriverWait时出现问题-InvalidArgumentException,python,selenium,Python,Selenium,我想遍历某些元素,然后单击它们。在这个过程中,有人建议我使用Selenium的WebDriverWait,但我遇到了一些困难,在尝试了一段时间后,我还没有弄清楚 我的代码: # finds all heart elements hearts = driver.find_elements_by_xpath("//span[@class='fr66n']") for h in range(len(hearts)): try: element = WebDriverWait(

我想遍历某些元素,然后单击它们。在这个过程中,有人建议我使用Selenium的WebDriverWait,但我遇到了一些困难,在尝试了一段时间后,我还没有弄清楚

我的代码:

# finds all heart elements
hearts = driver.find_elements_by_xpath("//span[@class='fr66n']")

for h in range(len(hearts)):
    try:
        element = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, hearts[h])));
        ActionChains(driver).move_to_element(hearts[h]).click(hearts[h]).perform()
        counter += 1
        print(str(counter) + "/" + str(len(hearts)))
    except exceptions.StaleElementReferenceException as e:
        raise e
遇到错误:

selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: 'value' must be a string
它指向这条线:

element = WebDriverWait(driver, 10).until(
      EC.element_to_be_clickable((By.XPATH, hearts[h])));

根据猜测,我假设它指的是hearts[h]应该是一个字符串,但它不是已经存在了吗?希望我的解释是错误的,有人有更好的想法。谢谢。

hearts[h]
是一个
,但您可以使用它作为Xpath定位器
(By.Xpath,hearts[h])
来使用索引选择元素

xpathIndex = "(//span[@class='fr66n'])[{}]".format(h+1) # xpath index start from 1 not 0
# (//span[@class='fr66n'])[1]
element = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, XpathIndex)));
ActionChains(driver).move_to_element(hearts[h]).click(hearts[h]).perform()
# or
# ActionChains(driver).move_to_element(element).click(element).perform()

我建议用hearts[h]代替str(hearts[h]),看看这是否是问题所在