This is the 17th day of my participation in the August More text Challenge. For details, see:August is more challenging

One problem with Selenium, whether applied to automated tests or crawlers, is login. We’ve talked about many, many different ways to find elements and use them. Here’s how to apply it today.

Selenium’s methods for finding elements include ID, classname, tag, xpath, and selector. It doesn’t really matter which one you use, as long as you’re adept at finding the elements. The operation of a web page is generally click and input, and there is no more complicated operation.

# Create WebDriver object, Chrome(r' your own local path ') # Call the Webdriver object's get method to make the browser open the specified url Driver.get ('https://www.baidu.com') # Select element by id, Element = driver.find_element_by_id('kw') # Enter a string into the input box element. Element. Send_keys (' How to learn selenium \n')Copy the code

Remember I said before this section of baidu search code? Generally speaking, web login is also account and password login. In fact, we just need to find the element of the account, find the element of the password, respectively, enter, the above example is sufficient. Then go to the login button and click it automatically. And the auto-click function is.click(). Any meta-control with this function will execute successfully.

The above is all case 1. In this case, there is no verification code, but some logins need verification code, or even two-dimensional code. What we can do is log in using cookies.

How do we add cookies in Selenium?

driver.add_cookie({'name':'xxxx','value':'xxxx'})
Copy the code

The above section is the use of cookies. In Selenium, cookies are added section by section. Then add the necessary file headers so you can bypass the login directly.

Above are the more common solutions to bypass the login problem. If you are interested in learning Selenium, you can follow the public account: poe-like code, leave a message to me, and I will teach you to study systematically.