Sign in with Python friend QQ Space likes

Source: Internet
Author: User
Tags xpath selenium chromedriver

Remember before with my female ticket said, said to help her space praise, point to 999 will not point. At first can remember every day, but later things a lot, will inevitably forget, the first two days of praise when suddenly feel so good boring ah, just also learning python, in the thought can have what method can automatically praise.

Before learning C with Win32API also did the operation of other applications, think the feasibility is still very high, so went to Baidu a bit. Found that there is a similar, say dry, just the weekend, that is directly up late "to" up. First on the code:

1 defQqzan (QQ):2Browser =Webdriver. Chrome ()3 Browser.maximize_window ()4Browser.get ('Https://user.qzone.qq.com/{}/main'. Format (QQ))5Browser.switch_to_frame ('Login_frame')6     #simulate input and click button submissions by using selectors to select to form elements7BROWSER.FIND_ELEMENT_BY_ID ('Switcher_plogin'). Click ()8BROWSER.FIND_ELEMENT_BY_ID ('u'). Clear ()9BROWSER.FIND_ELEMENT_BY_ID ('u'). Send_keys (' here to fillyour QQ account')TenBROWSER.FIND_ELEMENT_BY_ID ('P'). Clear () OneBROWSER.FIND_ELEMENT_BY_ID ('P'). Send_keys (' here to fillyour QQ password') ABROWSER.FIND_ELEMENT_BY_ID ('Login_button'). Click () -Time.sleep (3) -Browser.find_element_by_xpath ('//*[@id = "like"]/a[1]'). Click () theBrowser.quit ()

Here defines a function, enter the QQ number can directly login TA's space to point to praise. I am using the Chrome browser to operate, it is recommended that you use Firefox. If everyone and I are using chrome, need to download a corresponding version of your selenium chromedriver.exe, need to download can go to csdn to see, be sure to download the corresponding version. After downloading, put Chromedriver.exe in your Python installation directory.

The code is simple, open the Chrome browser, maximize the window, enter the space you want to access, enter your account and password to log in, click the Like button, and then close the browser. To note that, I am here is a lite version, no judgment did not do exception processing, such as may you open the Web page, but some things did not load out you will directly find the input, certainly can not find, the end will not succeed, generally wait a few seconds, or determine whether it has been loaded successfully (with dynamic wait). There are many ways to locate, there is not much to say, the Internet is very clear, you first open the browser, the mouse to the corresponding position, such as the input box, right click, open check, such as:

  

Through these basic can be located, some do not have these attribute, but also through Xpath, such as positioning, the operation is as follows: Before checking the found code there right-->> copy-->> Copy Xpath (other similar)

The complete code is as follows:

1 defQqzan (QQ):2Browser =Webdriver. Chrome ()3 Browser.maximize_window ()4Browser.get ('Https://user.qzone.qq.com/{}/main'. Format (QQ))5Browser.switch_to_frame ('Login_frame')6     #simulate input and click button submissions by using selectors to select to form elements7BROWSER.FIND_ELEMENT_BY_ID ('Switcher_plogin'). Click ()8BROWSER.FIND_ELEMENT_BY_ID ('u'). Clear ()9BROWSER.FIND_ELEMENT_BY_ID ('u'). Send_keys (' Fill in your QQ number here')TenBROWSER.FIND_ELEMENT_BY_ID ('P'). Clear () OneBROWSER.FIND_ELEMENT_BY_ID ('P'). Send_keys (' Fill in your QQ password here') ABROWSER.FIND_ELEMENT_BY_ID ('Login_button'). Click () -Time.sleep (3) -Browser.find_element_by_xpath ('//*[@id = "like"]/a[1]'). Click () the browser.quit () -Qqzan ('want to point to the QQ number of praise')

Sign in with Python friend QQ Space likes

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.