Automate Zhihu Login with Selenium Java via Third‑Party Authentication
This guide shows how to use Selenium WebDriver in Java to bypass Zhihu's captcha by logging in through a third‑party account, handling multiple windows and an iframe, and then returning to the original page.
While learning Selenium with Java, the author wanted to capture Zhihu's homepage recommendations or organize high‑quality answers, but the captcha on the login page was cumbersome. By using a third‑party login (QQ), the process can be automated.
The script performs the following steps:
Navigate to https://www.zhihu.com/ and click the "登录" (Login) button.
Click the element that triggers third‑party login.
Store the current window handle as homehandle for later return.
Click the QQ login button identified by the class name sprite-index-icon-qq.
Collect all window handles, iterate through them, and switch to the newly opened QQ login window (the handle that does not equal homehandle), then maximize the window.
Switch into the login iframe ptlogin_iframe.
Click the specific QQ account element with id img_out_1009329307 to select the user.
After authentication, iterate over the stored handles again, switch back to the original Zhihu window (where the handle equals homehandle), and maximize it.
The only notable complication is the presence of an iframe during the QQ login step; otherwise, the automation flow is straightforward.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
