A little trick makes it easy for Selenium to deal with scrollbars and element focus

01. JS processing scroll bar

Execute via webdriver_ The script () method executes the JS script operation scroll bar

Right scroll bar

  • Firefox browser and Chrome browser use different syntax. It is listed in the following area. It looks like Google browser 71 general Firefox
# Firefox browser scrolls to the bottom
js = "var q=document.documentElement.scrollTop=10000"



# Firefox browser scrolls to the top

js = "var q=document.documentElement.scrollTop=0"

  
# Chrome browser scrolls to the bottom

js = "var q=document.body.scrollTop=10000"



# Chrome browser scrolls to the top

js = "var q=document.body.scrollTop=0"



# Cut in the scroll bar of the embedded form through the id attribute

js = "var q=document.getElementById('id').scrollTop=0"

Horizontal scroll bar:

#  x is the transverse distance and y is the longitudinal distance

js = "window.scrollTo(x,y);"

It is said that there is no compatibility problem with the scrollTo function

  • – scrollHeight gets the scroll height of the object.

  • – scrollLeft sets or gets the distance between the left boundary of the object and the leftmost end of the currently visible content in the window.

  • – scrollTop sets or gets the distance between the top of the object and the top of the visible content in the window.

  • – scrollWidth gets the scrollWidth of the object

# Scroll to bottom

js = "window.scrollTo(0,document.body.scrollHeight)"



# Scroll to top

js = "window.scrollTo(0,0)"



# Scroll to a position where the lateral distance is X and the longitudinal distance is Y

js = "window.scrollTo(x,y)"

Execute js script

driver.execute_script(js)

Last code:

# coding=utf-8
# author:Ven
     
from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()
driver.set_window_size(width=800,height=700)
driver.get("https://www.baidu.com")
    driver.find_element_by_xpath("//input[@id='kw']").send_keys('selenium')
    driver.find_element_by_xpath("//input[@id='su']").click()
    sleep(1)
    js = "window.scrollTo(0,document.body.scrollHeight)"
    driver.execute_script(js)
    sleep(1)
    js = "window.scrollTo(500,0)"
    driver.execute_script(js)
    sleep(1)
    driver.quit()

02. Element focusing

Let the page jump directly to the position where the element appears through JS (pay attention to the covering phenomenon, focus on the covered elements and focus on the nearby elements)

target = driver.find_element_by_xpath()
driver.execute_script("arguments[0].scrollIntoView();", target)
# coding=utf-8
# author:Ven
     
from selenium import webdriver
from time import sleep
     
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
    driver.find_element_by_xpath("//input[@id='kw']").send_keys('selenium')
    driver.find_element_by_xpath("//input[@id='su']").click()
driver.set_window_size(width=600, height=600)
sleep(2)
target = driver.find_element_by_xpath("//*[@id='page']/a[1]/span[2]")
driver.execute_script("arguments[0].scrollIntoView();", target)             # Element focus to flip button - 2
sleep(2)
driver.quit()

Finally, I also share a piece of software testing data

The above contents should be the most comprehensive and complete preparation warehouse for software testing friends. In order to better sort out each module, I also refer to many high-quality online blogs and projects, and strive not to miss every knowledge point. Many friends review with these contents and get offer s from BATJ and other major manufacturers. This warehouse has also helped a lot of people Software testing learners, I hope it can also help you.

Pay attention to my WeChat official account: programmers two black, free access!

The most difficult time is when we are not far from success! If you don't want to experience the feeling that you can't find materials when you study by yourself, no one answers questions, and give up after a few days, you can join our group: 785128166. Let's discuss, exchange and learn together.

Highlights:

Working in Ali for 6 years, the voice of a 29 year old female software testing engineer

Ali p8, the new comer of the company, read the APP and interface test I did and gave me this document

Tencent two sides: web testing problems were abused and cried until the senior gave me these knowledge points

Keywords: Firefox Selenium chrome

Added by freaka on Tue, 07 Dec 2021 15:11:09 +0200