selenium执行JavaScript语句:控制滚动条 聚焦元素 改变下拉选项

1.

执行js脚本 控制滚动条
# http://www.cnblogs.com/yoyoketang/p/6128655.html
In [347]: js = "window.scrollTo(document.body.scrollWidth,document.body.scrollHeight)"  #/2中间
In [348]: driver.execute_script(js)
driver.execute_script("window.scrollTo(0,0)")
通过聚焦focus
In [383]: target=driver.find_element_by_link_text(u'帮助')  #底部按钮
In [384]: driver.execute_script("arguments[0].scrollIntoView();", target)

2.

#coding:utf-8
from selenium import webdriver

def main():
    driver = webdriver.Firefox()
    # 改变网页显示文字
    # 网页源代码:
    # <html>
    # <body>
        # <div id="result"></div>
        # <script>document.getElementById("result").innerText = 'Hello World';</script>
    # </body>
    # </html>
    # driver.get('http://example.webscraping.com/places/default/dynamic')    
    # what = raw_input('what:')
    # driver.execute_script("document.getElementById('result').innerText = '%s'"%what)   
    # input(":")
    
    
    driver.get('http://example.webscraping.com/places/default/search')
    driver.find_element_by_id('search_term').send_keys('.')
    # 将下拉第二个选项改为1000
    driver.execute_script("document.getElementById('page_size').options[1].text = '1000'")
    driver.find_element_by_id('search').click()
    driver.implicitly_wait(30)
    links = driver.find_elements_by_css_selector('#results a')
    countries = [link.text for link in links]
    # driver.close()
    print countries
    print len(countries)


if __name__ == '__main__':
    main()

 

posted @ 2017-07-02 19:02  my8100  阅读(1211)  评论(0编辑  收藏  举报