2

I'm trying to load a URL which has dynamic HTML. The selenium webdriver loads the URL fine, but it appears to be searching for elements before the HTML has fully rendered. I tried WebDriverWait, but that does not appear to work.

Interestingly, after selenium fails to find the element, I can manually use the Chrome developer tools to find the element successfully.

Any ideas?

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

chromepath = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"
url        = "https://www.msci.com/end-of-day-data-search"
delay      = 15 #seconds

br = webdriver.Chrome(chromepath)
br.get(url)

try:
    br.find_element_by_class_name("accept-btn").click()
    print("accepted terms and conditions")
finally:
    try:
        labelxpath   = "//*[@id='form-content']/table[1]/tbody/tr[2]/td/span"
        labelElement = WebDriverWait(br, delay).until(lambda br: br.find_element_by_xpath(labelxpath))
        print(labelElement)
    except:
        print("could not find label")
        br.close()

Script results: "accepted terms and conditions" "could not find label"

1 Answer 1

2

Actually, it's not only about waiting for the page to load.

The element you are looking for is inside an iframe. You need to switch to it's context before searching for the element:

driver.switch_to.frame(0)  # 0 here is an index - using 0 since there is only one iframe
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you that fixed everything

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.