最近在学自动化测试,碰到一个问题,需要将选择的元素置顶后,在截屏
经过研究,用下面的代码可以实现!
1 from selenium import webdriver 2 import time 3 4 #mobileEmulation = {'deviceName': 'iPhone X'} 5 options = webdriver.ChromeOptions() 6 #options.add_experimental_option('mobileEmulation', mobileEmulation) 7 8 driver = webdriver.Chrome(chrome_options=options) 9 10 driver.get("https://www.baidu.com/s?ie=utf-8&f=3&rsv_bp=1&rsv_idx=1&tn=baidu&wd=%E4%B8%AD%E5%9B%BD&oq=%25E6%2597%25A2%25E5%25BC%2582%25E6%2583%25B3%25E5%25A4%25A9%25E5%25BC%2580%252C%25E5%258F%2588%25E5%25AE%259E%25E4%25BA%258B%25E6%25B1%2582%25E6%2598%25AF&rsv_pq=f9ec2fac00077ab3&rsv_t=01d6r%2BFmI1y%2Blk42v3PTdi0FCA%2B%2BeYwMY0EDDiajlBx0Rq6W2%2FdrXmBal4k&rqlang=cn&rsv_enter=1&inputT=1454&rsv_sug3=10&rsv_sug1=7&rsv_sug7=100&rsv_sug2=0&rsv_sug4=1454") 11 12 #driver.set_window_size(100, 800) 13 14 element = driver.find_element_by_id("7") 15 driver.execute_script("document.documentElement.scrollTop = arguments[0].offsetTop", element) 16 17 time.sleep(10) 18 driver.save_screenshot("d:\\test.png") 19 20 time.sleep(10) 21 driver.quit()