0

I'm trying to display text in what seems to be a container in web using selenium webdriver for python. Here is the inspect element -

<div style="overflow-x: hidden;">
  <div class="view-container" style="flex-direction: row; 
  transition: all 0s ease 0s; transform: translate(0%, 0px); direction: ltr; 
  display: flex; will-change: transform;">
    <div aria-hidden="false" data-swipeable="true" style="width: 100%; flex-
    shrink: 0; overflow: auto;">
      <div>
        <div class="qs-text">What is the answer to this question?</div>

I want it to display "What is the answer to this question"

I'm trying to use the below, but it doesn't return anything. -

element = driver.find_element_by_class_name("qs-text").text
print(element)

I tried find_element_by_css_selector("qs-text").text but that didn't help either. Can you please tell me what I'm doing wrong

Andersson
  • 51,635
  • 17
  • 77
  • 129
Manu
  • 1
  • 1
    *"but it doesn't return anything"* Do you mean you just get an empty string or exception raised? – Andersson Dec 11 '18 at 11:47
  • Maybe this other thread will help you: https://stackoverflow.com/questions/28346240/selenium-not-setting-input-field-value – Jeppe Spanggaard Dec 11 '18 at 12:21
  • @Andersson - I get an empty string when I use find_element_by_class and the below error when I use find_element_by_css_selector - Message: no such element: Unable to locate element: {"method":"css selector","selector":"qs-text"} – Manu Dec 12 '18 at 04:33
  • @Manu try `driver.find_element_by_class_name("qs-text").get_attribute('textContent')` – Andersson Dec 12 '18 at 05:04
  • This worked! Thank you @Andersson – Manu Dec 12 '18 at 05:52

1 Answers1

0

How about this:

element = driver.find_element_by_xpath("//*[contains(@class, 'qs-text') and contains(text(), 'answer')]")

print(element)
Alichino
  • 1,668
  • 2
  • 16
  • 25