Scrollbar operation : when the element to be operated in a page element, but the outer region, to be operated to scroll among the visible region
Steps:
1. To go first element to be operated
element = driver.find_element_by_xxxx ( "XXXX")
2. Then the slide element to the visible region; javaScript realized by executing the statement
driver.execute_script ( "arguments [0] .scrollIntoView ();", Element)
# another way, scrollIntoViewIfneeded
3. The operating element further
element.XXX ()
The following are four ways to move
Moving the object to the element 1. The element "bottom", "bottom" and the current window alignment driver.execute_script("arguments[0].scrollIntoView(false);",element) |
"Top" element 2. Move to the target element "top" and the current window alignment driver.execute_script("arguments[0].scrollIntoView();",element) |
3. Move to the bottom of the page driver.execute_script("window.scrollTo(0,document.body.scrollHeight)") |
4. Move to the top of the page driver.execute_script("window.scrollTo(0,document.body.scrollHeight,0)")
|
from Selenium Import the webdriver from selenium.webdriver.common.keys Import Keys # keyboard packet from selenium.webdriver.common.action_chains Import ActionChains # mouse packet from selenium.webdriver.support.wait Import WebDriverWait # latency packet Import Time # Time package Driver = webdriver.Firefox () driver.get ( ' http://www.baidu.com ' ) WebDriverWait (Driver, 10) .until ( the lambda X: x.find_element_by_id (' Kw ' )) driver.find_element_by_id ( ' kw ' ) .send_keys ( ' selenium ' , Keys.ENTER) # Search selenium and hit enter # positioning 'Next' element of the time.sleep (2 ) ELE = driver.find_element_by_class_name ( ' n- ' ) # slidably the 'Next' to the bottom of the page elements driver.execute_script ( " arguments [0] .scrollIntoView (); " , ELE) # click on 'Next' ele.click () Time. SLEEP ( 2 ) driver.execute_script ( "the window.scrollTo (0, document.body.scrollHeight) " ) # moves to the bottom of the time.sleep (2 ) driver.quit ()