2

I am trying to get text (marked by hashtags).

<div class="XYZ">
        <h5>
    "
          #######Reports due by##############
    "
          <span class="hbl" data-hint="task due date">
            <i class="icon-boxy-sign"></i>
          </span>
        </h5>
        <script type="jsv#61^"></script><script type="jsv#123_"></script>
        <script type="jsv#60^"></script><script type="jsv#124_"></script>
      <script type="jsv#59^"></script><p>#################07/10/2020#######################</p><script type="jsv/125^"></script>
    <script type="jsv/52_"></script><script type="jsv/24^"></script>
        <script type="jsv/42_"></script><script type="jsv/23^"></script>
      </div>

Python line to get the text inside the hashtags:

txt = dat =wait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'div[class="XYZ"]'))).text

I expect the line to print: "Reports due by" and "07/10/2020, I keep getting timeoutException and Unable to locate element errors.

5
  • HTML code pls, atleast some. We can't help u without it. Commented Sep 10, 2019 at 21:20
  • Edited just a few seconds before you asked :) Commented Sep 10, 2019 at 21:21
  • Try div.XYZ instead of div[class="XYZ"] (see class selector docs) Commented Sep 10, 2019 at 21:35
  • div.XYZ not working either. And I don't think that python syntax! Commented Sep 10, 2019 at 21:50
  • is the element inside frame? Commented Sep 12, 2019 at 15:41

2 Answers 2

1

Seems you were close. To extract the text (marked by hashtags) you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    print(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.XYZ"))).get_attribute("title"))
    
  • Using XPATH:

    print(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='XYZ']"))).get_attribute("title"))
    
  • 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
    

Here you can find a relevant discussion on selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium

Sign up to request clarification or add additional context in comments.

3 Comments

I have the correct imports. Neither expression above works for this particular situation. CSS_SELECTOR returns NONE Type, and XPATH times out! Is there a way to access the strings by their tags? <h5> and <p>. I think that's the only hope left.
I also have the correct wait "from selenium.webdriver.support.ui import WebDriverWait as wait" thanks
Marking this solution because it shows good response effort, and will help others. Thanks.
0

Change By. CSS_SELECTOR to By.XPATH and update locator to '//div[@class='XYZ']'. Should work.

1 Comment

Tried: (By.XPATH, '//div[@class='XYZ']). No Luck!

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.