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

5
  • 1
    "but it doesn't return anything" Do you mean you just get an empty string or exception raised? Commented Dec 11, 2018 at 11:47
  • Maybe this other thread will help you: stackoverflow.com/questions/28346240/… Commented Dec 11, 2018 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"} Commented Dec 12, 2018 at 4:33
  • @Manu try driver.find_element_by_class_name("qs-text").get_attribute('textContent') Commented Dec 12, 2018 at 5:04
  • This worked! Thank you @Andersson Commented Dec 12, 2018 at 5:52

1 Answer 1

0

How about this:

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

print(element)
Sign up to request clarification or add additional context in comments.

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.