This is the second day of my participation in Gwen Challenge
I’ll cut the crap and get right to it
Create a new project
To install selenium
Selenium is a browser automation testing tool.
Console input
pip install selenium
Copy the code
Download the Chromedriver
Browser driver, code to open the browser must have a browser driver, different versions of the browser driver is not the same
Download address: chromedriver.storage.googleapis.com/index.html
View the version in Google Chrome
I am the development version of Google Chrome browser, so far the latest driver only has version 91 driver, fortunately it can be adapted, otherwise I have to reinstall
Open npm.taobao.org/mirrors/chr…
Feel free to unzip to a directory
Now let’s try using code to open the browser and accessblog.csdn.net/tangcv
Create the openChromedriver.py file
Select the project and right click
Write the code
from selenium import webdriver
# drive address
path="D: / python3.9.5 chromedriver. Exe"
browser = webdriver.Chrome(path)
# Open the page
browser.get('https://blog.csdn.net/tangcv')
Copy the code
Select the file name and right-click
Automatically opens the browser and accesses the target pageblog.csdn.net/tangcv
Page text box input content, and click query
Since it’s a script, we can’t just visit the page, there must be a click event
Enter the page blog.csdn.net/tangcv to check the page source code, we find that the id of the tag is search-blog-words; The query button tag has an attribute class called bTN-search-blog
Query the attribute whose ID is search-blog-words and enter “JINGdong punctuality kill script”.
Browser.find_element_by_id (“search-blog-words”).send_keys(” jd.com spot-kill script “)
Query button CLSSS label named bTN-search-blog, click the button
browser.find_element_by_class_name(‘btn-search-blog’).click()
Input content, click query. The newly modified code is
from selenium import webdriver
# drive address
path="D: / python3.9.5 chromedriver. Exe"
browser = webdriver.Chrome(path)
# Open the page
browser.get('https://blog.csdn.net/tangcv')
browser.find_element_by_id("search-blog-words").send_keys("Jingdong punctuality kill script")
browser.find_element_by_class_name('btn-search-blog').click()
Copy the code
Run the code again
That’s all for today, and I’ll see you next time