1

I'm trying to do a simple Python Selenium automation on a website while the website is blocked by a dialog which needs to scroll down to see all the paragraph so as to pass into the website.enter image description here

I tried to use the code below to scroll the paragraph, but unsuccessful.

driver = webdriver.Chrome('chromedriver')
driver.maximize_window()
driver.implicitly_wait(30)
driver.get('https://www.fidelity.com.hk/en/our-funds/mpf')
wait = WebDriverWait(driver, 20)

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'div[data-action="button68"]'))).click()

time.sleep(1)
ele =driver.find_element_by_css_selector('.content-scrolling-behavior')
driver.execute_script("return arguments[0].scrollIntoView(true);", ele)

html capture enter image description here

I would appreciate any feedback on how to consistently select an option from the dropdown noted in the code provided. And here is the website I looking at: https://www.fidelity.com.hk/en/our-funds/mpf

2 Answers 2

1

This should work

ele =driver.find_element_by_css_selector('div.content-scrolling-behavior')
driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", ele)

UPD
Try this instead:

ele =driver.find_element_by_css_selector('div.container')
driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", ele)
Sign up to request clarification or add additional context in comments.

4 Comments

I couldn't see that long pop-up on my browser, so I can't be sure about the element locator. But if your locator is correct this will work.
what is the error? or just doesn't work without errors?
it doesn't show any error.. I've add "html capture" above, please take a look
I have updated locator. see if this will work now
1

You can scroll using ActionChain like this :

also, in that div, there are 27 li tags, so I am doing xpath indexing and then one by one I am moving driver focus to those li.

Sample code :

driver.implicitly_wait(30)
driver.maximize_window()
driver.get("https://www.fidelity.com.hk/en/our-funds/mpf")
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'div[data-action="button68"]'))).click()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.container")))
list_size = len(wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//ul[@class='list']/li"))))
print(list_size)
j = 1
for i in range(list_size):
    ActionChains(driver).move_to_element(wait.until(EC.visibility_of_element_located((By.XPATH, f"(//ul[@class='list']/li)[{j}]")))).perform()
    j = j + 1
    time.sleep(1)

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[class$='btn-confirm']"))).click()

1 Comment

This method can scroll the paragraph down but can't scroll to the bottom so that clicking the confirm button.

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.