1

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.

1
  • I would suggest to replace hearts[h] for str(hearts[h]) to see if thats the problem Commented Jan 2, 2019 at 3:23

1 Answer 1

2

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.