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

How to learn Selenium and use Selenium to simulate login to Zhihu

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to learn Selenium and use Selenium simulation login Zhihu, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Now it's time to learn to use Python for dynamic crawlers, and Selenium is a tool for dynamic crawlers.

Introduction to Selenium

As we all know, the content of many websites need to log in and get their content, so we need to log in first, so we have this simulated login article.

To simulate login, we need to use the selenium automated testing tool, which needs to be installed separately. If you are using pycharm, you can go to setting and click to install selenium.

Then download the browser driver, here I recommend using Firefox, because compared to Chrome, it has no version restrictions and is not error-prone. Firefox can be downloaded directly by Baidu, there is a recommended download, Firefox driver can also be installed after Baidu Geckodriver download, finally, remember to set environment variables, otherwise it will bring a series of troubles and errors, so be sure to set up here.

Webdriver.Firefox () this is the driver to get the Firefox browser

Element .click () this is the click event of a control that simulates a web page

Element .clear () clears the value of the element

The element .send _ keys (username) assigns a value to the element

Element positioning

Let's talk about the positioning methods of elements, there are many ways to locate elements, let me list below, these methods can understand the meaning by looking at the words.

Find_element_by_id

Find_element_by_name

Find_element_by_xpath

Find_element_by_link_text

Find_element_by_partial_link_text locates through partial hyperlink text

Find_element_by_tag_name

Find_element_by_class_name

Find_element_by_css_selector

Window switching

Toggle window

Driver.switch_to_window ('windowname')

Toggle frame

Driver

Pop-up window processing

Alert = driver.switch_to_alert ()

Alert.dismiss

Use Selenium to simulate login to Zhihu

# coding=utf-8

Import os

From selenium import webdriver

# user name and password of Zhihu

Username = "XXXXXXX"

Password = "XXX"

#, get the browser driver. Here, you need to specify the environment variable to firefox in advance. If not, you need to specify the path.

Driver = webdriver.Firefox ()

# window maximization

Driver.maximize_window ()

# Open the login page

Driver.get ("https://www.zhihu.com/signup?next=%2F")"

# switch to the login page

Driver.find_element_by_xpath ('/ / * [@ id= "root"] / div/main/div/div/div/div [2] / div [2] / span') .click ()

# assign values to the input box

Driver.find_element_by_xpath ('/ / * [@ id= "root"] / div/main/div/div/div/div [2] / div [1] / form/div [1] / div [2] / div [1] / input') .send_keys (username)

Driver.find_element_by_xpath ('/ / * [@ id= "root"] / div/main/div/div/div/div [2] / div [1] / form/div [2] / div/div [1] / input') .send_keys (password)

# simulate click events

Driver.find_element_by_xpath ('/ / * [@ id= "root"] / div/main/div/div/div/div [2] / div [1] / form/button') .click ()

Print driver.title

Os.system ("pause")

Description: this kind of simulated login is not complete, because it does not involve the operation of CAPTCHA, which is even more complicated if it is designed to input CAPTCHA for login. I will explain it further later.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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

Internet Technology

Wechat

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

12
Report