The foreword 0.

Recently, I am very interested in Python. I mainly want to use it and ADB to do some plug-in for Android games, and write some small crawlers to solve the content problems on the server side. Selenium is an automated testing tool that manipulates the click and assignment of browser mock elements.

1. The analysis

To open the nuggets website, click on the icon in the upper right corner

Selenium is used in Selenium

2.1 Obtaining drivers

Webdriver = webdriver.chrome () gets the driver for the Chrome browser

2.2 Element Positioning

find_element_by_id find_element_by_name find_element_by_xpath find_element_by_link_text Find_element_by_partial_link_text Locate find_element_by_tag_name find_element_by_class_name by partial hyperlinks find_element_by_css_selector

2.3 Element Events

Element. Click () simulates the element’s click event element. Clear () clarifies the element’s value element. Send_keys (content) assigns the value to the element

2.4 Switching Window

webdriver.switch_to.window(handle)

2.5

3. Code implementation

def login(webdriver, url, login_xpath, username_xpath, user_name, password_xpath, password, login_btn_xpath):
    webdriver.get(url)
    if login_xpath is not None:
        login_btn = webdriver.find_element_by_xpath(login_xpath)
        login_btn.click()
        login_by_password(webdriver, username_xpath, user_name, password_xpath, password, login_btn_xpath)
    else:
        login_by_password(webdriver, username_xpath, user_name, password_xpath, password, login_btn_xpath)
        
    
        
def loginJueJin(webdriver, user_name, password, url='https://juejin.cn',
                login_xpath='//*[@id="juejin"]/div[2]/div/header/div/nav/ul/li[4]/span[1]',
                username_xpath='//*[@id="juejin"]/div[1]/div[3]/form/div[2]/div[1]/div[1]/input',
                password_xpath='//*[@id="juejin"]/div[1]/div[3]/form/div[2]/div[1]/div[2]/input',
                login_btn_xpath='//*[@id="juejin"]/div[1]/div[3]/form/div[2]/button'):
    login(webdriver, url, login_xpath, username_xpath, user_name, password_xpath, password, login_btn_xpath)L      
Copy the code

Follow my official account