Friday, April 20, 2018

Unable to make a split screen scroll to the bottom

Leave a Comment

I've written a script in python in combination with selenium to make the screen of a webpage scroll downward. The content is within the left-sided window. If I scroll down, more items are visible. I've tried with the below approach but It doesn't seem to work. Any help on this will be highly appreciated.

Check out this: website link.

What I've tried so far with:

import time from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.keys import Keys  driver = webdriver.Chrome() wait = WebDriverWait(driver, 10) driver.get("find_the_link_above")  elem = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#pannello-espositori .luogo")))  for item in range(3):     elem.send_keys(Keys.END)     time.sleep(3)  driver.quit() 

When I execute the above script, it throws an exception can't focus element.

4 Answers

Answers 1

Try to use the following method for that:

def scroll_down():     """A method for scrolling down the page."""      # Get scroll height.     last_height = driver.execute_script("return document.querySelector('#pannello-espositori').scrollHeight;")      while True:          # Scroll down to the bottom.         driver.execute_script("window.iScrollElenco.scrollBy(0, -arguments[0]);", last_height)          # Wait to load the page.         time.sleep(2)          # Calculate new scroll height and compare with last scroll height.         new_height = driver.execute_script("return document.querySelector('#pannello-espositori').scrollHeight;")         if new_height == last_height:              break          last_height = new_height 

Use this method when you want to scroll down content (using the height of the left side panel) in the left side panel.

Hope it helps you! Let me know about the result.

Answers 2

Try this. You can see scrolling effect by scrolling up to the elements in the left panel.

This solution would scroll up to first 100 elements.

from selenium import webdriver import time  def scroll_element_into_view(element):     driver.execute_script(         "arguments[0].scrollIntoView(true);",         element)     time.sleep(0.2) #increase/decrease time as you want delay in your view  driver = webdriver.Chrome() driver.maximize_window() driver.set_page_load_timeout(5) try:     driver.get("http://catalogo.marmomac.it/it/cat")     time.sleep(3)     total_elems= driver.find_elements_by_css_selector(".scroller .elemento")     print len(total_elems)     for i in range(len(total_elems)):         scroll_element_into_view(total_elems[i]) except Exception as e:     print e finally:     driver.quit() 

As you have mentioned, after scroll it would load more elements.Below script would handle that too. Here we can use total count which already shown at top of the panel.

for ex count is : 1669

  1. First it will scroll from 1 to 100 element
  2. Again find total elements which is now 150
  3. So it will scroll from 101 to 150
  4. Again find total elements which is now 200
  5. So it will scroll from 150 to 200

this process would continue till 1669 element. (Store previous count in one variable and update it after every loop)

try:     driver.get("http://catalogo.marmomac.it/it/cat")     time.sleep(3)     total_elems=0     total_count = int(driver.find_element_by_css_selector(".totali").text)     while total_elems<total_count:         elems= driver.find_elements_by_css_selector(".scroller .elemento")         found_elms= len(elems)         for i in range(total_elems,found_elms):             scroll_element_into_view(elems[i])         total_elems=found_elms except Exception as e:     print e finally:     driver.quit() 

Answers 3

Have you tried something like:
Option 1

execute_script("arguments[0].scrollIntoView();",element) 

Option 2

from selenium.webdriver.common.action_chains import ActionChains  element = driver.find_element_by_id("my-id")  actions = ActionChains(driver) actions.move_to_element(element).perform() 

Answers 4

The selector "#pannello-espositori .luogo" gives part of the text in the first element in the panel. If you scroll down, this element is no longer visible and might not be able to get any more commands. You can locate the entire list and use ActionChains to scroll to the last element each time. It will be scrolled into view and will reload the list

actions = ActionChains(driver) for item in range(3):     elements = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "#pannello-espositori .elemento")))     actions.move_to_element(elements[-1]).perform() 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment