3

Python + Selenium: I can't get print text from this div:

<div id="modal-content-18" class="modal-content" data-role="content">
    <div>
    SignUp Failed. Please Try Again.
    </div>
</div>

I tried this:

resp = browser.find_element_by_class_name("modal-content").text
print resp

But it does not work.

Please help me.

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
SENTOT SAROYO
  • 39
  • 1
  • 2
  • have you tried a query by id ? `"modal-content-18"` – PRMoureu Sep 14 '19 at 14:50
  • to go off @PRMoureu comment, there could be more than one element with that class name. perhaps query by ID, or wrap in for loop if there are many elements with that class name. – BLang Sep 14 '19 at 14:55
  • That element itself does not have any text. The next div, nested inside of it, has the text. – John Gordon Sep 14 '19 at 15:01
  • What does "does not work" mean? Does it print the wrong thing? Does it crash? Does it throw an error? If it throws an error, what's the error? – Bryan Oakley Sep 15 '19 at 17:30

3 Answers3

2

I personally prefer xpaths because of cases like these. They can tackle many complex cases as well. Try the following:

resp = browser.find_element_by_xpath('//div[@class="modal-content"]/div').text
print resp

In case the element isn't visible on the screen. The text method will be none. In that case you need the textContent attribute. Use the following then:

resp = browser.find_element_by_xpath('//div[@class="modal-content"]/div').get_attribute("textContent")
print resp

Let me know if it works for you. Also make sure there is only one modal-content on the page. In case there are more than one, your css_selector is insufficient to identify this element. To check this you can run the following.

l = len(browser.find_elements_by_xpath('//div[@class="modal-content"]/div'))
print l

if it returns a number greater than 1, then the modal-content class alone isn't enough and you will need to expand on your selection criteria.

0

Induce WebDriverWait and visibility_of_element_located() and following locator strategy.

Using CLASS_NAME:

print(WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.CLASS_NAME,"modal-content"))).text)

Using XPATH:

print(WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,"//div[@class='modal-content' and @data-role='content']"))).text)

You need to import followings.

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

EDITED Check the textContent attribute value.

print(WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.CLASS_NAME,"modal-content"))).get_attribute("textContent"))

OR

print(WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,"//div[@class='modal-content' and @data-role='content']"))).get_attribute("textContent"))
KunduK
  • 32,888
  • 5
  • 17
  • 41
0

The desired text SignUp Failed. Please Try Again. is within the child <div> so you have to induce WebDriverWait for the desired 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, "div.modal-content[id^='modal-content-'][data-role='content']>div"))).get_attribute("innerHTML"))
    
  • Using XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='modal-content' and starts-with(@id, 'modal-content-')][@data-role='content']/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 title attribute through Selenium using Python?


Outro

As per the documentation:

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