zoukankan      html  css  js  c++  java
  • 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()
  • 相关阅读:
    Unity shader 代码高亮+提示
    PTA --- L2-003 月饼
    PTA --- L2-002 链表去重
    计蒜客 —— 字符串p型编码
    计蒜客 —— 最好的草
    最近忙科研立项 & 对博客的优化
    计蒜客 —— 删除单词后缀
    Tensorflow 保存模型 & 在java中调用
    Tensorflow 用训练好的模型预测
    Tensorflow 保存和载入训练过程
  • 原文地址:https://www.cnblogs.com/my8100/p/7106817.html
Copyright © 2011-2022 走看看