0

I'm trying to extract data from the link below using selenium via python: www.oanda.com

But I'm getting an error that, "Unable to Locate an Element". In browser console i tried using this Css selector:

document.querySelector('div.position.short-position.style-scope.position-ratios-app')

This querySelector returns me the data for short percentage of 1st row in the browser console(for this test), but when i used this selector in the python script below it gives me an error that, "Unable to Locate element" or sometimes empty sctring. Please suggest me solution if there's any.Will be grateful, thanks :)

# All Imports
import time
from selenium import webdriver

#will return driver
def getDriver():
    driver = webdriver.Chrome()
    time.sleep(3)
    return driver


def getshortPercentages(driver):
    shortPercentages = []
    shortList = driver.find_elements_by_css_selector('div.position.short-position.style-scope.position-ratios-app')
    for elem in shortList:
        shortPercentages.append(elem.text)
    return shortPercentages

def getData(url):
    driver = getDriver()
    driver.get(url)
    time.sleep(5)
    # pagesource = driver.page_source
    # print("Page Source: ", pagesource)
    shortList = getshortPercentages(driver)
    print("Returned source from selector: ", shortList)


if __name__ == '__main__':
    url = "https://www.oanda.com/forex-trading/analysis/open-position-ratios"
    getData(url)

1 Answer 1

1

Required data is located inside an iframe, so you need to switch to iframe before handling elements:

driver.switch_to.frame(driver.find_element_by_class_name('position-ratios-iframe'))

Also note that data inside iframe is dynamic, so make sure that you're using Implicit/Explicit wait (using time.sleep(5) IMHO is not the best solution)

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

1 Comment

thanks it's working. was working on that from recent 4hours and you save my day. Many thanks.

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.