首页 > 代码库 > selenium手动执行JavaScript语句

selenium手动执行JavaScript语句

#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()

 

selenium手动执行JavaScript语句