Some notes on using selenium in Python

Source: Internet
Author: User

#coding: Utf-8
From selenium import Webdriver
From selenium.webdriver.common.action_chains import Actionchains #引入ActionChains鼠标操作类
From Selenium.webdriver.common.keys import keys #引入keys类操作
Import time


def main ():
Browser = Webdriver. Chrome (' D:\\phantomjs\\chromedriver.exe ')


#程序片段一: Open Baidu homepage for related information
Browser.get (' http://www.baidu.com ')
Browser.maximize_window () # Maximize the browser
Print browser.find_element_by_name (' tj_trhao123 '). Text
Print Browser.title # page title
Print Browser.page_source # page All code
Time.sleep (10)
Return


# program Fragment two: Open the Baidu homepage, click the link through the mouse
Browser.maximize_window ()
Browser.get (' http://www.baidu.com ')
Article = browser.find_element_by_link_text (U ' map ')
Actionchains (browser). move_to_element (article). Click (). Perform ()
Time.sleep (10)
Return


# program Snippet three: Open Baidu homepage, enter keywords and click the search button
Browser.get (' http://www.baidu.com ')
browser.find_element_by_id (' kw '). Send_keys (U ' Chinese ')
Print browser.find_element_by_id (' kw '). Get_attribute (' type ')
Print browser.find_element_by_id (' kw '). Size # Printing Input Box sizes
browser.find_element_by_id (' su '). Click ()
Time.sleep (10)
Return


# program Fragment four: Operating keyboard example
Browser.get (' http://www.renren.com/SysHome.do ')
browser.find_element_by_id (' email '). Clear () # This is the element selected with the ID
browser.find_element_by_id (' email '). Send_keys (' email ')
browser.find_element_by_id (' email '). Send_keys (Keys.back_space)
browser.find_element_by_id (' email '). Send_keys (Keys.enter)
Time.sleep (10)
Return


if __name__ = = ' __main__ ':

Main ()

Attention:

1, download and storage chromedriver, its version and Google Chrome browser, this example is stored in the D disk in a directory

2. Need to install selenium module in Python

3. Need to download and install PHANTOMJS software

4. Four program fragments can be run separately, you can comment out the code that does not need to run





Some notes on using selenium in Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.