2

I want to login to Zhihu, a Chinese Q&A site. The login URL is https://www.zhihu.com/signin?next=%2F. Click on the second tab with text 密码登录 to view the login form. I wanted to login to it, but when I submit the form with all information correctly, it fails. There is a sign_in request with status 403 when I submit the form. I've tried to set the window.navigator.webdriver option to true, setting add_experimental_option("excludeSwitches", ["enable-automation"]), or even using pyppeteer, but none of them work. Any ideas for this? Thanks in advance!

UPDATE:

My code is below:

from selenium.webdriver import Chrome, ChromeOptions

options = ChromeOptions()
# options.add_argument('--headless')
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = Chrome(options=options)
# Enable the script below to get rid of the `webdriver` property in JavaScript
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
    "source": """
        Object.defineProperty(navigator, 'webdriver', {
        get: () => null
        })
    """
})


def login(username, password):
    # Error that may be raised while executing
    error = None
    try:
        driver.get('https://www.zhihu.com/signin?next=%2F')
        psw = driver.find_elements_by_class_name('SignFlow-tab')[1]
        psw.click()
        username_input = driver.find_element_by_name('username')
        username_input.send_keys(username)
        password_input = driver.find_element_by_name('password')
        password_input.send_keys(password)
        btn = driver.find_element_by_class_name('SignFlow-submitButton')
        btn.click()
        print('Please check the captcha.')
    except Exception as err:
        error = err
    finally:
        # Raise the error if detected
        if error:
            print(str(error))


login(input('Username: '), input('Password: '))

PS: I'm using ChromeDriver version 84.

Sam Zhang
  • 320
  • 4
  • 17

0 Answers0