[python] The ninth day of clock-in learning - selenium automation operation browser (below)


Event address: CSDN 21-day learning challenge

The biggest reason for learning is to get rid of mediocrity. One day earlier, there will be more splendor in life; Dear friends, if you:
want to systematically/deeply learn a certain technical knowledge point...
it is difficult to persist in learning alone, and want to learn efficiently in a group...
want to write a blog but can't start, and urgently need to inject energy into writing dry goods...
love writing, willing to let yourself become better people

...

Welcome to participate in the CSDN Learning Challenge and become a better self. Please refer to the free high-quality column resources of the high-quality column bloggers in the event (this part of the high-quality resources is free and open for a limited time in the event~), according to your own learning field and learning progress Learn and document your own learning process. You can choose one of the following three aspects to start (not mandatory), or publish column learning works according to your own understanding, as follows:

**

study diary

**
1. Learning knowledge points

APIs of selenium

2. Problems encountered in learning

API not touched

3. Learning gains

Selenium's api usage

4. Practical operation

No interface mode:

from selenium import webdriver

# 1. Instantiate the configuration object
chrome_options = webdriver.ChromeOptions()
# 2. Add the configuration object to enable the headless command
chrome_options.add_argument('--headless')
# 3. Add the configuration object to disable the gpu command
chrome_options.add_argument('-- disable-gpu')
# 4. Instantiate a browser object with a configuration object
browser = webdriver.Chrome(chrome_options=chrome_options)

browser.get('https://www.baidu.com/')

# View the requested data
print(browser.page_source) # View the rendered data, you can parse it with Xpath to get the data
print(browser.get_cookies()) # View the cookie value after the request page
print(browser.current_url) # View request url

# Close the page
browser.close()
# Close the browser
browser.quit()

get id:

from selenium.webdriver.common.by import By

# Get the first element
browser.find_element(by=By.ID, value="list-1")
# Get multiple elements
browser.find_elements(by=By.ID, value="list-1")

Get text:

ret = browser.find_element_by_class_name('element')
print(ret[0].text)

Web page forward and backward:

# Forward
browser.forward()

# Back 
browser.back()

ip proxy:

from selenium import webdriver
import time

# 1. Instantiate configuration object
options = webdriver.ChromeOptions()
# 2. Add command using proxy ip to configuration object
options.add_argument('--proxy-server=http://ip address') # Proxy IP: port number
# 3. Instantiate the driver object with the configuration object
driver = webdriver.Chrome(chrome_options=options)
driver.get("https://www.baidu.com")

# Get page content
print(driver.page_source)

# Close the current window after a delay of 3 seconds, if it is the last window, exit
time.sleep(3)
driver.close()

Request header modification:

from selenium import webdriver
import time

agent = 'Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1' # 1. Instantiate configuration object options
=
webdriver.ChromeOptions ()
# 2. Configuration object modify request header
options.add_argument('--user-agent=' + agent)
# 3. Instantiate driver object with configuration object
driver = webdriver.Chrome(chrome_options=options)
driver.get ("https://www.baidu.com")

# Get page content
print(driver.page_source)

# Close the current window after a delay of 3 seconds, if it is the last window, exit
time.sleep(3)
driver.close()

Thanks:

Thanks to Mr. Wuxian for his guidance. Through the study and understanding during this period of time, I have more experience in the practical application of python. It is recommended that friends who have zero foundation and are interested move to the Wuxian column for systematic learning. 

Guess you like

Origin blog.csdn.net/qq_34217861/article/details/126453529
Recommended