简体   繁体   中英

Selenium WebDriver Unable to Find Element by Id, Using Python

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/fxdriver@googlecode.com/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")

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.

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM