selenium scroll bar drop-down operation in python

Method 1) Use js script to operate directly, the method is as follows:

js="var q=document.getElementById('id').scrollTop=10000"
driver.execute_script(js)
or:

js="var q=document.documentElement.scrollTop =10000"
driver.execute_script(js)


The id here is the id of the scroll bar, but there is no xpath method in js, so this method is not applicable for pages with no id in the scroll bar

Method 2) Use js script to drag to the

target target = driver.find_element_by_id("id_keypair")
driver.execute_script("arguments[0].scrollIntoView();", target) #Drag to a visible element
This method can drag the scroll bar to the position of the element to be displayed, This method has a wide range of uses, you can use

Method 3) according to the page display, send the tab key

. In the page in this example, the password is the input box. During normal manual operation, the tab key can be used to switch to the password box, so According to this idea, you can also send the tab key to switch in python, so that the element is displayed

from selenium.webdriver.common.keys import Keys
driver.find_element_by_id("id_login_method_0").send_keys(Keys. TAB)




===================================================== == #Move
the scroll bar to the bottom of the page
js="var q=document.documentElement.scrollTop=100000" 
driver.execute_script(js) 
time.sleep(3) #Move 
the scroll bar to the top of the page 
js="var q=document.documentElement.scrollTop=0" 
driver.execute_script(js) 
time.sleep(3) #To 
operate the scroll bar in the embedded window in the page, first locate the embedded window, Scroll bar operation
js="var q=document.getElementById('id').scrollTop=100000"
driver.execute_script(js)
time.sleep(3)

This is said to work for IE and firefox, but not for chrome!


No need to position, just pull down the scroll bar bit by bit (firefox, chrome is absolutely feasible)
length=1000
for i in range(0,2):
    js="var q=document.body.scrollTop="+str(length) 
    driver . 
    time.sleep(3)
    length+=length


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326350294&siteId=291194637