I tried scraping printing text from div and get error as shown below! please help
from selenium import webdriver
import pandas as pd
driver = webdriver.Chrome(executable_path= '/Users/leah/Documents/Scrap-Finantials/webdrivers/chromedriver')
driver.get('https://www.dummy-id.com/StockInfo/_ScreenerScan.html?SYMBOLS_PP=25&SYMBOLS_ONLY=on&sort1=MaxVWVP&sort2=None&sort3=None&WN=Lowfloat&MaxFloat=20000000&XN=on&X_NYSE=on&X_ARCA=on')
ticker = driver.find_elements_by_xpath('//body/div[4]/div/div/div/div[1]/div')
len(ticker)
print(ticker.text)
#Try printing ticker and get following error!
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-45-0ca4276d61ba> in <module>
----> 1 print(ticker.text)
AttributeError: 'list' object has no attribute 'text'
CodePudding user response:
Calling find_elements_by_xpath()
will get a list of elements, not a single element. Calling len(ticker) is fine, but ticker.text is not correct.
if you want to print each single element, you need a loop
ticker = driver.find_elements_by_xpath('//body/div[4]/div/div/div/div[1]/div')
len(ticker)
for elem in ticker:
print(elem.text)
Reference: https://selenium-python.readthedocs.io/locating-elements.html