简体   繁体   中英

Selenium can't get the text from element

I can't get the text from the element. I think it is a dynamically added text (from Angular) to the element and therefore not loaded directly in the element. The text inside the element is in the format of eg "3" with citation marks around ut.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

import xlsxwriter

import re
pattern = r"[\"\d{1, 2}\"]"

PATH = "C:\Program Files (x86)\chromedriver.exe"
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
driver = webdriver.Chrome(PATH, chrome_options=chrome_options)


driver.get("some-url")

xpathPain = "/html/body/div[2]/div/div/div[1]/div/div/div[1]/div[3]/div/div/div[1]/div[3]/development-numbers/status-numbers/div/div[2]/div/h4"

try:
    element = WebDriverWait(driver, 20).until(
        EC.presence_of_element_located((By.XPATH, xpathPain)))

    elementPain = driver.find_element_by_xpath(xpathPain)
    print(elementPain.text)

except TimeoutException:
    print("Failed to load elementPain")

I get the output: (blank, like an empty string)

. I have tried to wait til the text is loaded with the EC text_to_be_present_in_element(locator, text_) and tried to use a regular expression for the text part.

The page source for the element is:

<h4 class="status-numbers__number">
"6"
<!---->
</h4>

So how do I get the number 6 from this element?

I have tried print(elementPain.get_attribute("innerHTML")) and that gets the "<!---->" part of the text but not the '"6"' part. I have also tried .getAttribute("innerText"), .getAttribute("textContent").

I have tried using the firefox geckodriver instead as well. No result.

I have managed to solve the issue using Firefox and this code:

try:
    element = WebDriverWait(driver, 20).until(
        EC.element_to_be_clickable((By.XPATH, xpathPain)))

    elementPain = driver.find_element_by_xpath(xpathPain)
    print(elementPain.get_attribute("innerHTML"))

Don't know it it had to do with the element out of viewport.

Use the following XPath to identify the element. You can use element.text or element.get_attribute("textContent") to get the text.

try:
    WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//h4[@class='status-numbers__number']")))

    elementPain = driver.find_element_by_xpath("//h4[@class='status-numbers__number']")
    
    print(elementPain.text) #To get the text using text
    
    print(elementPain.get_attribute("textContent")) #To get the text using get_attribute()

except TimeoutException:
    print("Failed to load elementPain")

I have managed to solve the issue using Firefox and this code:

try:
    element = WebDriverWait(driver, 20).until(
        EC.element_to_be_clickable((By.XPATH, xpathPain)))

    elementPain = driver.find_element_by_xpath(xpathPain)
    print(elementPain.get_attribute("innerHTML"))

Don't know it it had to do with the element out of viewport.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM