Reputation: 15
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)
Upvotes: 0
Views: 120
Reputation: 193338
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
Upvotes: 0
Reputation: 4507
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)
Upvotes: 1