首页 > 解决方案 > Unable to open href link in selenium python

问题描述

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)

标签: selenium

解决方案


您正在获取 Web 元素的列表,因此您需要首先href从这些 Web 元素中获取属性,然后点击它们。
你可以这样做:

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)

推荐阅读