019: Selenium operates Boss direct employment for one click position delivery

This is not advertising. There are many good recruitment platforms. Boss direct employment is one. Although it is practical for Boss direct employment to communicate directly with HR, the delivery position is very troublesome and needs to be clicked manually one by one. Most of the communication has not been responded. So I wrote an automatic communication script with Selenium + Python today. When writing, I found that the anti Selenium measures above Boss direct employment were also in place.

Let me introduce the specific steps of code implementation.

First simulate login:

Boss direct employment official website: https://www.zhipin.com/ url of the login interface I directly visit: https://login.zhipin.com/?ka=header-login

It seems that he can save some effort by opening here and logging in directly with his account and password.

I want to use class_ When name gets the input directly, it finds that there are three mobile phone number input boxes. Here, you need to use xpath to locate the element,

So right click input, select Copy, and click Copy XPath in copy.

The same is true for the password box. Direct copy XPath

driver.find_element_by_xpath('//*[@id="wrap"]/div[2]/div[1]/div[2]/div/form/div[3]/span[2]/input').click()
driver.find_element_by_xpath('//*[@id="wrap"]/div[2]/div[1]/div[2]/div/form/div[3]/span[2]/input').send_keys('your phone number ')
time.sleep(1)
driver.find_element_by_xpath('//*[@id="wrap"]/div[2]/div[1]/div[2]/div/form/div[4]/span/input').click()
driver.find_element_by_xpath('//*[@id="wrap"]/div[2]/div[1]/div[2]/div/form/div[4]/span/input').send_keys('your password ')
time.sleep(1)
driver.find_element_by_xpath('//*[@id="wrap"]/div[2]/div[1]/div[2]/div/form/div[6]/button').click()
time.sleep(2)

Then there is this troublesome slider. Although this slider looks very simple. Just drag to the far right.

At the beginning, I directly used ActionChains to drag the mouse. I tried it here for a long time. Every time I dragged it to the end, I would report an error. It's very difficult here.

Then report an error in pycham: stale element reference: element is not attached to the page document

It shows that the obtained elements are changing =, = here can be matched with our xpath through a fixed id.

# id = driver.find_element_by_xpath('//*[@id="wrap"]/div[2]/div[1]/div[2]/div/form/div[5]/div[1]').get_attribute('data-nc-idx')
# print(id)
# time.sleep(0.5)
# huakuai = driver.find_element_by_xpath('//*[@id="nc_{}_n1z"]'.format(id))

However, after a long time, I found that this is not only the problem of element expiration, but also an error will be reported when I drag manually.

In this way, our WebDriver should be detected by the Boss.

So, we need to hide our webdriver attribute. There are several ways, such as adding a mitmproxy proxy. Here, I simply change selenium to the developer mode to prevent shielding based on webdriver.

from selenium.webdriver import ChromeOptions
option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
url = 'https://login.zhipin.com/?ka=header-login'
driver = webdriver.Chrome(executable_path=r'C:\Users\lenovo\Desktop\chromedriver_win32\chromedriver.exe',options=option)
driver.get(url)
Update 2020 / 05 / 03: Netizens found that the previous developer mode can't be used because the new version of Google this year cancelled the setting of webdriver = flash in the developer mode. Now add the following js:
    driver = webdriver.Chrome(executable_path=r'C:\Users\lenovo\Desktop\chromedriver_win32\chromedriver.exe',options=option)

    driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", 
                       { "source": """ Object.defineProperty(navigator, 'webdriver', { get: () => undefined }) """ 
                         })
    driver.get(url)
However, it is easy to report errors when operating the slider. What should I do... Simply filter out this slider. Anyway, our goal is to automatically deliver resumes, not to log in all the time.

Therefore, with the concept of one click operation in the title, we manually drag the verification code when logging in. Others do not need to be operated. (for now)

Automatic communication:

After logging in, you will automatically challenge the default search interface in the default region: There is no difficulty here. Send keywords directly and click search.

driver.find_element_by_xpath('//*[@id="main"]/div/div[2]/div[1]/div[1]/div/form/div[1]/p/input').click()
driver.find_element_by_xpath('//*[@id="main"]/div/div[2]/div[1]/div[1]/div/form/div[1]/p/input').send_keys('crawler ')
driver.find_element_by_xpath('//*[@id="main"]/div/div[2]/div[1]/div[1]/div/form/button').click()
time.sleep(1)

Then record the URL of the current page: liebiao_url = driver.current_url

Here, I'll move the mouse to the item we want to communicate:

ActionChains(driver).move_to_element(driver.find_element_by_xpath('//*
[@id="main"]/div/div[3]/ul/li[{}]/div/div[3]'.format(i))).perform()      # i give the loop outside, and the complete code is pasted to the end
	time.sleep(0.5)

Then get the url of the details page:

xiangqing_url =  driver.find_element_by_xpath('//*[@id="main"]/div/div[3]/ul/li[{}]/div/div[1]/h3/a'.format(i)).get_attribute('href')
print(xiangqing_url)
driver.get(xiangqing_url)
time.sleep(1)

Click "communicate with me" on the details page:

driver.find_element_by_xpath('//*[@id="main"]/div[1]/div/div/div[3]/div[1]/a').click()
time.sleep(1)

Return to the previous list page:

driver.back()
time.sleep(1)
driver.back()
time.sleep(1)
if driver.current_url != liebiao_url:
   driver.get(liebiao_url)

Sometimes this thing will appear, and it will be wrong to back up twice. So we have to judge that if this happens, we can only back once.

 if 'https://www.zhipin.com/job_detail' in str(driver.current_url):
     driver.back()
     time.sleep(1.5)
 else:
     time.sleep(1.5)
     driver.back()
     time.sleep(2)
     driver.back()
     time.sleep(2)

Test results:

It's done. You can wait for a reply while watching others! If you need to know how much you have delivered and what position to say hello to, you can get the next element on the details page to get detailed information. I won't say more.

Full code:

from selenium import webdriver
from selenium.webdriver import ActionChains
import time
from selenium.webdriver import ChromeOptions

option = ChromeOptions()
# driver = Chrome(options=option)
url = 'https://login.zhipin.com/?ka=header-login'
driver = webdriver.Chrome(executable_path=r'C:\Users\lenovo\Desktop\chromedriver_win32\chromedriver.exe',options=option)

driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", 
                   { "source": """ Object.defineProperty(navigator, 'webdriver', { get: () => undefined }) """ 
                     })
driver.get(url)

def huakuai():
# id = driver.find_element_by_xpath('//*[@id="wrap"]/div[2]/div[1]/div[2]/div/form/div[5]/div[1]').get_attribute('data-nc-idx')
# print(id)
# time.sleep(0.5)
# huakuai = driver.find_element_by_xpath('//*[@id="nc_{}_n1z"]'.format(id))
# action = ActionChains(driver)
# action.click_and_hold(huakuai).perform()
# time.sleep(1)
# for i in range(12):
#     action.move_by_offset(i, 0).perform()
# action.release().perform()
# action.release(on_element=huakuai).perform()
    pass
time.sleep(5)

driver.find_element_by_xpath('//*[@id="wrap"]/div[2]/div[1]/div[2]/div/form/div[3]/span[2]/input').click()
driver.find_element_by_xpath('//*[@id="wrap"]/div[2]/div[1]/div[2]/div/form/div[3]/span[2]/input').send_keys('13401108846')
time.sleep(1)
driver.find_element_by_xpath('//*[@id="wrap"]/div[2]/div[1]/div[2]/div/form/div[4]/span/input').click()
driver.find_element_by_xpath('//*[@id="wrap"]/div[2]/div[1]/div[2]/div/form/div[4]/span/input').send_keys('ying5338619')
time.sleep(1)
driver.find_element_by_xpath('//*[@id="wrap"]/div[2]/div[1]/div[2]/div/form/div[6]/button').click()
time.sleep(2)

driver.find_element_by_xpath('//*[@id="main"]/div/div[2]/div[1]/div[1]/div/form/div[1]/p/input').click()
driver.find_element_by_xpath('//*[@id="main"]/div/div[2]/div[1]/div[1]/div/form/div[1]/p/input').send_keys('crawler ')
driver.find_element_by_xpath('//*[@id="main"]/div/div[2]/div[1]/div[1]/div/form/button').click()
time.sleep(1)
liebiao_url = driver.current_url+'&page={}'
time.sleep(1)
for j in range(1,10):
    liebiao_url=liebiao_url.format(j)
    print(liebiao_url)
    driver.get(liebiao_url)
    time.sleep(1)
    print(driver.current_url)
    print("==========================")
    # print("list link", liebiao_url)
    # try:
    for i in range(1,30):
        if j==1:
            ActionChains(driver).move_to_element(driver.find_element_by_xpath('//*[@id="main"]/div/div[3]/ul/li[{}]/div/div[3]'.format(i))).perform()
            time.sleep(0.5)
            xiangqing_url = driver.find_element_by_xpath('//*[@id="main"]/div/div[3]/ul/li[{}]/div/div[1]/h3/a'.format(i)).get_attribute('href')
            # print(xiangqing_url)
        else:
            ActionChains(driver).move_to_element(
                driver.find_element_by_xpath('//*[@id="main"]/div/div[2]/ul/li[{}]/div/div[3]'.format(i))).perform()
            time.sleep(0.5)
            xiangqing_url = driver.find_element_by_xpath('//*[@id="main"]/div/div[2]/ul/li[{}]/div/div[1]/h3/a'.format(i)).get_attribute('href')

        driver.get(xiangqing_url)
        time.sleep(1)
        driver.find_element_by_xpath('//*[@id="main"]/div[1]/div/div/div[3]/div[1]/a').click()
        time.sleep(0.5)
        print(driver.current_url)
        if 'https://www.zhipin.com/job_detail' in str(driver.current_url):
            driver.back()
            time.sleep(1.5)
        else:
            time.sleep(1.5)
            driver.back()
            time.sleep(2)
            driver.back()
            time.sleep(2)

Posted on Mon, 22 Nov 2021 09:05:44 -0500 by coolfool