What is the most efficient way to get the same attribute of multiple elements using Python, Selenium and PhantomJS? My solution uses find_elements_by_css_selector
which locates all the elements I need, which takes less than a second, then I loop through the list to get the attribute I require. My looping takes over a minute with around 2500 elements, which seems like a lot to me considering all the elements are mapped with find_elements_by_css_selector
method. Is get_attribute
method really that expensive or am I doing something wrong?
from selenium import webdriver
driver = webdriver.PhantomJS(executable_path=r'mypath\phantomjs.exe')
driver.set_window_size(1120, 550)
driver.get("https://www.something.com")
table = []
elements = driver.find_elements_by_css_selector("tr[id*='bet-']") # takes under 1 second
for element in elements:
table.append(element.get_attribute('data-info')) # takes over 60 seconds (2000 elements)
driver.close