selenium執行JavaScript語句:控制滾動條 聚焦元素 改變下拉選項

1.css

執行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.html

#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()
相關文章
相關標籤/搜索