0

I have a html code which shows time.

<span class="inner-2pZN4z">09:37:24 (UTC-4)</span>

Now this will change every second to show the correct time (I think it uses javascript to update the time, because I don't see any class change.). Now I want to take the time in my python code. So, I executed:

driver.find_elements_by_xpath("//span[@class='inner-2pZN4z']").text

This throws an exception:

Message: stale element reference: element is not attached to the page document

Edit

I went through How to disable Javascript when using Selenium? but disabling JavaScript for me will not work as there are other codes which need JavaScript.

Question: How can we disable/enable JavaScript in while executing python code using selenium chromedriver?

11
  • try with //span[contains(@class,'2pZN4z')], u can also try giving some wait before if page not loaded Commented May 6, 2021 at 13:45
  • @muraliselenium I think it uses javascript to update the time, because I don't see any class change. So contains will not solve I think Commented May 6, 2021 at 13:49
  • It seem that page updated by JS each second, so element becomes stale. You might need to disable JS on WD start or try this solution to disable it in runtime. P.S. Note that @class in code is not the same as in HTML Commented May 6, 2021 at 14:55
  • The class was just copy-paste error while adding question to SO, JavaScript cannot be disabled, edit made to the question for the same @JaSON Commented May 6, 2021 at 16:00
  • 1
    try driver.execute_script('return document.querySelector(".inner-2pZN4z").textContent') Commented May 7, 2021 at 7:25

0

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.