0

I have a array called accounts which gets all the href's i want, i then want to open each of these, i have tried the following code

        accounts = self.driver.find_elements_by_xpath("//a[contains(@href, '/signin?')]")
        for account in accounts:
            self.driver.get(account)
            time.sleep(3)

But returns

selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: 'url' must be a string
  (Session info: chrome=80.0.3987.132)
1
  • You can try to print(account) to see variable account. Commented Mar 16, 2020 at 8:12

2 Answers 2

1

You are fetching the list of web elements, so you need to first fetch the href attribute from those web elements and then hit them.
You can do it like:

accounts = self.driver.find_elements_by_xpath("//a[contains(@href, '/signin?')]")
for account in accounts:
    self.driver.get(account.get_attribute("href"))
    time.sleep(3)
Sign up to request clarification or add additional context in comments.

1 Comment

Be careful to address relative urls in the hrefs as well.
0

To open the href, instead of the WebElement you need to invoke get() passing the href attribute inducing WebDriverWait for the visibility_of_all_elements_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    accounts = [my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "a[href*='/signin?']")))]
    for account in accounts:
        self.driver.get(account)
    
  • Using XPATH:

    accounts = [my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.XPATH, "//a[contains(@href, '/signin?')]")))]
    for account in accounts:
        self.driver.get(account)
    
  • Note : You have to add the following imports :

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

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.