0

I'm trying to pull up an element that only gets created after the JavaScript runs, but I keep getting the following error message:

selenium.common.exceptions.NoSuchElementException: Message: u'Unable to locate element: {"method":"id","selector":"post-count"}' ; Stacktrace: Method FirefoxDriver.prototype.findElementInternal_ threw an error in file:///tmp/tmpittNsw/extensions/[email protected]/components/driver_component.js

I'm trying to pull this element up on cnn.com. My code:

socket.setdefaulttimeout(30)
browser = webdriver.Firefox() # Get local session of firefox
browser.get(article_url_txt) # Load page

result = browser.find_element_by_id("post-count")
1
  • Maybe your not giving it time to finish loading, is there no onready event you can have call your find element? Commented Feb 24, 2013 at 2:53

2 Answers 2

1

The element you are looking for is inside an iframe.

The following did the trick for me:

from selenium.webdriver.support.wait import WebDriverWait

# ...

frame = WebDriverWait(browser, 30).until(lambda x: x.find_element_by_id("dsq1"))
browser.switch_to_frame(frame)
result = WebDriverWait(browser, 30).until( lambda x: x.find_element_by_id("post-count"))

Note that I included the use of WebDriverWait(...).until(...) to allow the elements to be created dynamically just in case.

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

Comments

0

You can tell the WebDriver to wait implicitly until the element is visible.

browser.implicitly_wait(30)
result = browser.find_element_by_id("post-count")

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.