selenium common operations, js - to add elements to the page to scroll the visible area

  Our automated testing, will meet the needs of mobile browser page elements to the visible area, you need to use the page scroll up or down

js operation - a scroll bar:: Selenium used throughout execute_script (translation: Ax Q Patent Laid-Si splash strange.) method performed js statement;

 

Scroll to the visible area of ​​the page elements common operations:

1, "bottom" to the target elements in an element "bottom" and the current window is aligned:

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

2, "top", "top" and the current window is moved to align the elements of the object element:

  • 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, moves to the top of the page:

  • driver.execute_script("window.scrollTo(document.body.scrollHeight,0)")

 

Scroll to the element of visible area: Baidu query "computer", the "computer _ Baidu Encyclopedia" moving in the end section.

1, need to scroll objects: first positioning element

2, js scrolling sentence: four methods above

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time

driver = webdriver.Chrome()
driver.get("http://www.baidu.com")

driver.find_element_by_id("kw").send_keys("电脑" , Keys.ENTER)
 # driver.find_element_by_id (" su "). The Click () 

# 1, find me want to scroll to the visible region of the element 
LOC = (By.XPATH, ' // A [text () =" _ Baidu Encyclopedia "] ' ) 
WebDriverWait (Driver, 20 is ) .until (EC.visibility_of_element_located (LOC)) 
element = driver.find_element (* LOC) 

# 2, the function will be performed js scroll element to the visible region : execute_script (translation: Exeter LANCE Q Unexamined Patent Si strange splash) 
driver.execute_script ( " arguments [0] .scrollIntoView (to false); " , Element)   # . because the mask has a top layer, the bottom of the visible area is aligned with the 
# execution of a function js, If two parameter 
#driver.execute_script ( "arguments [0] .scrollIntoView (to false); Alert (arguments [. 1])", Element, "200 is") 

# after scroll into the visible region, a tap operation 
element.click ()
 # wait five seconds, viewing, close the browser 
the time.sleep (5 ) 
driver.quit ()

 

 

******* Please respect the original, as to reprint, please indicate the source: Reprinted from: https://www.cnblogs.com/shouhu/ , thank you! ! ******* 

Guess you like

Origin www.cnblogs.com/shouhu/p/12227216.html