'Can't access text when using selenium with python - possibly disabled

I am having issues accessing the text saying '-175'.

website html (not sure how to post the screenshot!)

I failed when trying this:

driver.find_element_by_xpath('//div[@class="my-bets-history-possible-win ng-hide"]/span[2]').text

I have also enabled the disabled parts of the html by using this:

button = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//input[@class="second-color ng-pristine ng-untouched ng-valid ng-not-empty"]')))
driver.execute_script('arguments[0].removeAttribute("disabled");', button)

Any further ideas?



Solution 1:[1]

To extract the text -175 ideally you need to induce WebDriverWait for the presence_of_element_located() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR and text attribute:

    print(WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "div.my-bets-history-possible-win.ng-hide span::nth-of-type(2)"))).text)
    
  • Using XPATH and get_attribute("innerHTML"):

    print(WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//div[@class="my-bets-history-possible-win ng-hide"]//following::span[2]"))).get_attribute("innerHTML"))
    
  • 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


References

Link to useful documentation:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1