2
<div class="Fz(12px) Px(8px) Mend(4px) Va(m) Bdrs(3px) C($c-fuji-grey-g) Bgc($c-fuji-grey-b) Cur(d) Py(3px)"><svg class="Mend(6px) Cur(d) Cur(p)" 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')
  • The DOM line you provided apparently has a lot of randomized attributes. The only static attribute looks like is `data-icon="traffic"`. Also, it looks like it is enclosed in an `svg` element. it's difficult to narrow down the element without looking at the complete DOM that relates to this segment. If this is the only `svg` element in your page, then you may try `driver.find_element(By.XPATH, "//*[name()='svg']).text`. Also, instead of `presence_of_element_located`, try using `visibility_of_element_located` as sometimes element maybe present but not visible and hence extraction fails – Anand Gautam Feb 16 '22 at 13:14

2 Answers2

2

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

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.

ZhorGen
  • 41
  • 5
  • i tried but it gives me: AttributeError: 'list' object has no attribute 'text' – layzclassic Feb 16 '22 at 15:26
  • Because find_elements returns a list of all matching elements to your xpath. That's why I suggested to use find_element (without the S) and if the element you're looking for isn't the first one add another condition in your xpath. – ZhorGen Feb 17 '22 at 08:26