Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Python how to use selenium Simulation browser to enter the message function of my friend Qzone

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "python how to use selenium simulation browser to enter friend Qzone's message function". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "python how to use selenium simulation browser to enter friend Qzone's message function"!

First download the selenium module, pip install selenium, and download a browser driver (I use Google here).

# Import # Note the changes in the find_element () method of each version of python (python3.10) from selenium import webdriverfrom selenium.webdriver.chrome.service import Servicefrom selenium.webdriver.common.by import By# creates a mock browser object, and then manipulates the browser s=Service ("chromedriver.exe") browser=webdriver.Chrome (service=s) through the object.

Qzone uses the QR code to log in by default. We need to use the account password to log in. Note that the Qzone login box is in an iframe tag: locate the frame.

Browser.get ('https://qzone.qq.com/')browser.maximize_window()time.sleep(2)browser.switch_to.frame('login_frame')a_tag = browser.find_element (By.ID, "switcher_plogin") a_tag.click ()

The next step is to enter the account number, password and click to log in.

UserName_tag = browser.find_element (By.ID,'u') password_tag = browser.find_element (By.ID,'p') time.sleep (1) userName_tag.send_keys ('here is the QQ number') time.sleep (1) password_tag.send_keys ('here is the password') time.sleep (1) btn = browser.find_element (By.ID,'login_button') btn.click ()

The effect picture realized at present

The next step is to go to the friends page in the navigation bar above, locate the friend search box, and pass the friends to the search box.

Some iframe do not have id or name, and use xpath to locate

After logging in, browser.switch_to.default_content () # returns to the default framework time.sleep (1) browser.find_element (By.XPATH,'//* [@ id= "aMyFriends"]') .click () time.sleep (1) element1 = browser.find_element (By.XPATH,'// [@ id= "app_container"] / iframe') browser.switch_to.frame (element1) ff=browser.find_element (By.XPATH) '/ * [@ id= "qz-search-box-input"]') ff.send_keys (friend) time.sleep (1) browser.switch_to.default_content () element2=browser.find_element (By.XPATH,'// [@ id= "app_container"] / iframe') browser.switch_to.frame (element2) browser.find_element (By.XPATH '/ / * [@ id= "qz-search-box-result"] / li/div [2] / p') .click () time.sleep (1) browser.find_element (By.XPATH,'// [@ id= "mecarewho_list"] / li/div [2] / div [2] / pcine a') .click () time.sleep (1) # enter your friend's page

Achieve results:

The next step is to go to the friend's message board and leave a message.

Note that the message box and the publish button are on different frame, published on the outside floor, check carefully

Windows = browser.window_handlesbrowser.switch_to.window (windows [- 1]) time.sleep (1) browser.find_element (By.XPATH,'//* [@ id= "friendship_promote_layer"] / table/tbody/tr [1] / td [2] / a'). Click () time.sleep (1) # browser.execute_script ('window.scrollTo (0document. Body.scrollHeight)') browser.find_element (By.XPATH '/ / * [@ id= "menuContainer"] / div/ul/li [4]') .click () # or browser.find_element (By.XPATH "/ / div [@ id='layBackground'] / / li [@ class = 'menu_item_334'] / / a [text () =' message board']") .click () time.sleep (3) # enter the message board browser.switch_to.frame ('tgb') time.sleep (1) browser.switch_to.frame (' veditor1_Iframe') time.sleep (1) ff=browser.find_element (By.XPATH) '/ html/body') # message box ff.send_keys (word) browser.switch_to.default_content () browser.switch_to.frame (' tgb') dd=browser.find_element (By.XPATH,'//* [@ id= "btnPostMsg"]') dd.click () # confirm publish button print ("message successful!") Time.sleep (2) browser.quit ()

Python rookie, please give me a lot of advice on what is wrong.

The complete code is as follows:

From selenium import webdriverfrom selenium.webdriver.chrome.service import Servicefrom selenium.webdriver.common.by import Byimport timefrom selenium.webdriver import ActionChainsfrom selenium.webdriver import ChromeOptions# Please enter friend and message content qq=input ('enter your QQ number:') friend = input ('Please enter friend:') word = input ('Please enter message content:) # create a simulated browser object Then use the object to manipulate the browser option=ChromeOptions () option.add_argument ('- headless') option.add_argument ('--disable-gpu') s=Service ("chromedriver.exe") browser = webdriver.Chrome (service=s,options=option) browser.get ('https://qzone.qq.com/')browser.maximize_window()time.sleep(2) browser.switch_to.frame (' login_frame') a_tag = browser.find_element (By.ID) "switcher_plogin") a_tag.click () userName_tag = browser.find_element (By.ID,'u') password_tag = browser.find_element (By.ID,'p') time.sleep (1) userName_tag.send_keys (qq) time.sleep (1) password_tag.send_keys ('enter your password here') time.sleep (1) btn = browser.find_element (By.ID After logging in, 'login_button') btn.click () browser.switch_to.default_content () # returns to the default framework time.sleep (1) browser.find_element (By.XPATH,'//* [@ id= "aMyFriends"]') .click () time.sleep (1) element1 = browser.find_element (By.XPATH,'//* [@ id= "app_container"] / iframe') browser.switch_to.frame (element1) ff=browser.find_element (By.XPATH) '/ / * [@ id= "qz-search-box-input"]') ff.send_keys (friend) time.sleep (1) browser.switch_to.default_content () element2 = browser.find_element (By.XPATH,'//* [@ id= "app_container"] / iframe') browser.switch_to.frame (element2) browser.find_element (By.XPATH '/ / * [@ id= "qz-search-box-result"] / li/div [2] / p') .click () # difficult time.sleep (1) # search okbrowser.find_element (By.XPATH '/ / * [@ id= "mecarewho_list"] / li/div [2] / div [2] / pampa') .click () time.sleep (1) # enter friend # to get the first window handle windows = browser.window_handlesbrowser.switch_to.window (windows [- 1]) time.sleep (1) browser.find_element (By.XPATH) '/ / * [@ id= "friendship_promote_layer"] / table/tbody/tr [1] / td [2] / a') .click () time.sleep (1) # browser.execute_script (' window.scrollTo (0note.body.scrollHeight)') browser.find_element (By.XPATH,'//* [@ id= "menuContainer"] / div/ul/li [4]') .click () # or browser.find_element (By.XPATH "/ / div [@ id='layBackground'] / / li [@ class = 'menu_item_334'] / / a [text () =' message board']") .click () time.sleep (3) # enter the message board browser.switch_to.frame ('tgb') time.sleep (1) browser.switch_to.frame (' veditor1_Iframe') time.sleep (1) ff=browser.find_element (By.XPATH) '/ html/body') # message box ff.send_keys (word) browser.switch_to.default_content () browser.switch_to.frame (' tgb') dd=browser.find_element (By.XPATH,'//* [@ id= "btnPostMsg"]') dd.click () # confirm publish button print ("message successful!") Time.sleep (2) browser.quit () Thank you for your reading, the above is the content of "how python uses the selenium simulation browser to enter the friend Qzone message function". After the study of this article, I believe you have a deeper understanding of how python uses the selenium simulation browser to enter the friend Qzone message function, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report