Patriots299
Patriots299

Reputation: 365

Problem implementing WebDriverWait with Python - InvalidArgumentException

I want to iterate through certain elements and then click them. In the process, I was suggested to utilize Selenium's WebDriverWait, but I am facing some difficulties which after trying for a while, have not yet been able to figure out.

My code:

# 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

Error encountered:

selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: 'value' must be a string

It points to this line:

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

By guess, I assume it refers that hearts[h] should be a String, but isn't it already? Hopefully my interpretation is wrong and someone has a better idea. Thanks.

Upvotes: 1

Views: 377

Answers (1)

ewwink
ewwink

Reputation: 19164

hearts[h] is a <element> but you use it as Xpath locator (By.XPATH, hearts[h]) to select element using index you can do

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()

Upvotes: 2

Related Questions