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

标签 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(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]<element>但你将它用作 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()

关于python - 使用 Python 实现 WebDriverWait 时出现问题 - InvalidArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54000797/

相关文章:

python - 如何让easygui过滤文件列表

python - 如何在 Pytorch 中迭代层

python - 在同一事件循环中运行 Qt 和 Websockets 服务器

selenium - 无法使用 Appium Selenium Android 在 ListView 中滚动元素

python - 我如何在普通浏览器中使用 selenium

java - 如何在Java中使用Selenium Webdriver获取标签值

html - 如何为div标签的多个兄弟编写XPath

python tcp服务器向多个客户端发送数据

python - 禁用 Python 的 re.findall() 的 "group becomes tuple"行为

python - 使用 selenium(和 python)设置下载目录