<div ><svg width="18" height="18" viewBox="0 0 48 48" data-icon="traffic" style="vertical-align: middle; fill: rgb(51, 51, 51); stroke: rgb(51, 51, 51); stroke-width: 0;"><path d="M35.826 11.728c-1.102.044-1.96.975-1.918 2.078.045 1.102.975 1.96 2.078 1.918l3.73-.15.014.015-10.033 10.033L19.6 15.527c-.78-.78-2.047-.78-2.827 0-.142.142-.25.302-.338.47-.168.09-.33.197-.47.34L.585 31.714c-.78.78-.78 2.047 0 2.828.392.39.904.586 1.415.586s1.024-.196 1.414-.587L18.187 19.77l10.07 10.068c.39.39.9.586 1.413.586s1.024-.195 1.414-.586c.11-.11.2-.23.28-.355.167-.09.327-.197.468-.34l10.71-10.71-.148 3.7c-.023.58.204 1.112.585 1.493.343.343.81.563 1.333.584 1.104.044 2.035-.815 2.078-1.918l.44-11.003-11.004.438z"></path></svg>Bullish</div>
I am trying to extract the text,'Bullish', using XPATH but it is not working out.
sentiment_search = '//*[@id="canvass-0-CanvassApplet"]/div/ul/li/div/div[3]/div/div/text()'
sentiment = driver.find_elements(By.XPATH, sentiment_search).text
try:
for i in range(len(posts)):
try:
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, sentiment_search)))
if sentiment == 'Bullish':
print('Bullish')
CodePudding user response:
To extract the text Bullish you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#canvass-0-CanvassApplet > div > ul > li > div > div:nth-of-type(3) > div > div"))).text)
Using XPATH:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//*[@id="canvass-0-CanvassApplet"]/div/ul/li/div/div[3]/div/div"))).text)
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
You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python
CodePudding user response:
You don't need the "text()" in the xpath:
sentiment_search = '//*[@id="canvass-0-CanvassApplet"]/div/ul/li/div/div[3]/div/div'
and then get the text from the element:
sentiment = driver.find_elements(By.XPATH, sentiment_search).text
If there's only one element I suggest to use find_element instead of find_elements which returns list of all matching elements.