2
<div class="SdTitle-sc-iwgcvh dmugDV title">#24 Information Revolution</div>

I want to get the text in this div. I was using this code:

title = driver.find_element_by_xpath("/html/body/div/div[2]/div[4]/div/div[2]/div[1]/div/div[1]").get_attribute("innerText")

But it returns an empty string, I tried text(), get_attribute("innerText"), get_attribute("innerHTML"). None of them works.

The element can be found by selenium. I also found this element with the xpath in chrome, which means the xpath is correct.

If I tried get_attribute('outerHTML'), it returns

<div class="SdTitle-sc-iwgcvh dmugDV title"></div>

This is so strange.

I searched on Chrome that the div with these classes only has 1.

Thank you for your help.

2
  • Can you share HTML code in text format here? or if URL is public then please share Commented May 24, 2021 at 13:29
  • Here is the URL, apeuni.com/zh-CN/practice/essays/24 Commented May 24, 2021 at 13:30

4 Answers 4

2

you are using text() instead of .text

There is no such thing called get_attribute("innerText")

Update 1 :

el = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[class^='SdTitle']"))).text
print(el)
Sign up to request clarification or add additional context in comments.

6 Comments

I suspect the # makes selenium ignores the text.
I can not locate this in chrome /html/body/div/div[2]/div[4]/div/div[2]/div[1]/div/div[1]
is that element seen after login ?
Chrome Developer Tool / Elements (command F, input the full xpath)/
Working solution el = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[class^='SdTitle']"))).text print(el) presented by @cruisepandey
|
2

The attribute is called innerHTML Read here for more details: https://www.w3schools.com/jsref/prop_html_innerhtml.asp

el = driver.find_element_by_css_selector(".SdTitle-sc-iwgcvh.dmugDV.title").text
el = driver.find_element_by_css_selector(".SdTitle-sc-iwgcvh.dmugDV.title").get_attribute("innerHTML")

Print to check the output. A little explanation: . is used for a class name. If there are few classes in a one html tag, you can make a locator of them divided by dot, without spaces.

Comments

0

Try this

driver.find_element_by_xpath(".//div[@class='SdTitle-sc-iwgcvh dmugDV title']").get_attribute("innertext")

If not use text to get the text value from the element like this

driver.find_element_by_xpath(".//div[@class='SdTitle-sc-iwgcvh dmugDV title']").text

1 Comment

0

Similar issue, I found in Protractor. I too, used different ways to get the text using .getText(), innerText, innerHtml etc. But finally I tried to use tagName locator to retrieve the text. Might be below code is helps to you to implement in Selenium. Note: Below code is for Protractor

element(by.tagName('mat-chip')).getText().then(function (rowAtt) {
                console.log("Attributed Under Row Group-"+rowAtt)
                expect(rowAtt).toBe(attValues.explore.attributeValues.rowAttributes[i], 'Attribute value is not matching');
                if (rowAtt) {
                    exploreModule.attributeCheckBox().click();
                } else {
                    console.log("attribute checkbox is not present");
                }
            })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.