如何让Selenium WebDriver调用浏览器Cookie实现Tinder自动化登录?
问题描述
我正在编写Selenium脚本自动登录Tinder并执行操作,但WebDriver无法调用浏览器已存储的Cookie。点击“Log in with Facebook”后仍需输入邮箱、密码甚至手机验证码,想直接用浏览器保存的凭据跳过这些步骤,该怎么实现?

我的代码如下:
package Test; // Generated by Selenium IDE import org.junit.Before; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.After; import static org.junit.Assert.*; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.core.IsNot.not; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.Dimension; import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.Alert; import org.openqa.selenium.Keys; import java.util.*; import java.net.MalformedURLException; import java.net.URL; public class MounaTest { private static WebDriver driver; private Map<String, Object> vars; JavascriptExecutor js; @BeforeEach public void setUp() { driver = new ChromeDriver(); js = (JavascriptExecutor) driver; vars = new HashMap<String, Object>(); } @AfterAll public static void tearDown() { driver.quit(); } @Test public void test() { System.out.println("MOUNA CAMELIA"); driver.get("https://tinder.com/app/recs"); driver.manage().window().setSize(new Dimension(1552, 840)); driver.get("https://tinder.com/"); driver.manage().window().setSize(new Dimension(1552, 840)); driver.findElement(By.cssSelector(".Miw\(120px\) .l17p5q9z")).click(); { WebElement element = driver.findElement(By.cssSelector(".Miw\(120px\) .l17p5q9z")); Actions builder = new Actions(driver); builder.moveToElement(element).perform(); } { WebElement element = driver.findElement(By.tagName("body")); Actions builder = new Actions(driver); builder.moveToElement(element, 0, 0).perform(); } vars.put("window_handles", driver.getWindowHandles()); driver.findElement(By.cssSelector(".My\(12px\):nth-child(2) .Mend\(a\):nth-child(2)")).click(); vars.put("root", driver.getWindowHandle()); driver.switchTo().window(vars.get("win1725").toString()); driver.close(); driver.switchTo().window(vars.get("root").toString()); driver.findElement(By.cssSelector(".Mx\(a\):nth-child(4) path")).click(); driver.findElement(By.cssSelector(".Pos\(a\):nth-child(4) > .C\(\$c-ds-icon-secondary\) path")).click(); driver.findElement(By.cssSelector(".Mx\(a\):nth-child(4) path")).click(); { WebElement element = driver.findElement(By.cssSelector(".Mx\(a\):nth-child(4) path")); Actions builder = new Actions(driver); builder.moveToElement(element).perform(); } { WebElement element = driver.findElement(By.tagName("body")); Actions builder = new Actions(driver); builder.moveToElement(element, 0, 0).perform(); } driver.findElement(By.cssSelector(".Mx\(a\):nth-child(4) path")).click(); driver.findElement(By.cssSelector(".Pos\(a\):nth-child(4) > .C\(\$c-ds-icon-secondary\) path")).click(); driver.findElement(By.cssSelector(".Bgi\(\$g-ds-background-like\)\3A a .D\(b\)")).click(); { WebElement element = driver.findElement(By.cssSelector(".Bgi\(\$g-ds-background-like\)\3A a .D\(b\)")); Actions builder = new Actions(driver); builder.moveToElement(element).perform(); } { WebElement element = driver.findElement(By.tagName("body")); Actions builder = new Actions(driver); builder.moveToElement(element, 0, 0).perform(); } driver.findElement(By.cssSelector(".Mx\(a\):nth-child(4) path")).click(); } }
解决方案
原因说明
默认情况下,Selenium启动的是干净的浏览器实例,不会加载你本地浏览器已保存的Cookie、登录会话和用户配置,所以点击Facebook登录后还是需要重新验证。要实现复用本地登录状态,有两种可靠方案:
方案1:复用Chrome用户配置文件(推荐)
直接让WebDriver使用你平时登录Facebook和Tinder的Chrome配置文件,这样启动的浏览器会自动加载已保存的登录会话,无需重复验证。
步骤:
找到Chrome用户数据目录:
- Windows:
C:\Users\<你的用户名>\AppData\Local\Google\Chrome\User Data - Mac:
~/Library/Application Support/Google/Chrome - Linux:
~/.config/google-chrome
注意:替换
<你的用户名>为实际系统用户名- Windows:
修改代码中的
setUp方法:
添加ChromeOptions配置,指定用户数据目录和配置文件(通常是Default,如果你有多个用户配置,换成对应的文件夹名)。
修改后的setUp代码:
import org.openqa.selenium.chrome.ChromeOptions; @BeforeEach public void setUp() { ChromeOptions options = new ChromeOptions(); // 指定Chrome用户数据目录,注意最后不要加"/" options.addArguments("user-data-dir=C:\\Users\\你的用户名\\AppData\\Local\\Google\\Chrome\\User Data"); // 指定要使用的配置文件,默认是Default options.addArguments("profile-directory=Default"); driver = new ChromeDriver(options); js = (JavascriptExecutor) driver; vars = new HashMap<String, Object>(); }
注意事项:
- 运行脚本前,必须关闭所有手动打开的Chrome窗口,否则会因为配置文件被占用而报错。
- 如果不想影响日常使用的Chrome配置,可以复制一份
Default文件夹,重命名后使用,避免脚本操作污染你的日常浏览数据。
方案2:手动导出并导入Cookie
如果无法复用配置文件,可以手动导出已登录状态的Cookie,在脚本中导入,实现自动登录。
步骤:
手动获取Cookie:
- 打开Chrome,登录Tinder和Facebook,确保处于已登录状态。
- 按F12打开开发者工具,切换到
Application标签页,在左侧选择Cookies->https://tinder.com,右键点击列表,选择Copy all as JSON。
在脚本中导入Cookie:
先访问Tinder域名(必须先访问目标域名才能添加Cookie),然后将导出的Cookie逐个添加到WebDriver中,最后刷新页面。
示例代码片段:
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import org.openqa.selenium.Cookie; @Test public void test() { System.out.println("MOUNA CAMELIA"); // 先访问Tinder主页,确保域名匹配 driver.get("https://tinder.com/"); driver.manage().window().setSize(new Dimension(1552, 840)); // 导入Cookie(替换为你导出的JSON内容) String cookieJson = "[{\"name\":\"cookie_name1\",\"value\":\"cookie_value1\",\"domain\":\".tinder.com\",...}]"; try { List<Cookie> cookies = new ObjectMapper().readValue(cookieJson, new TypeReference<List<Cookie>>() {}); for (Cookie cookie : cookies) { driver.manage().addCookie(cookie); } } catch (Exception e) { e.printStackTrace(); } // 刷新页面,此时应该已经处于登录状态 driver.navigate().refresh(); // 后续操作... driver.findElement(By.cssSelector(".Miw\(120px\) .l17p5q9z")).click(); // ...其余代码保持不变 }
注意事项:
- Cookie有有效期,过期后需要重新导出。
- 部分网站(比如Facebook)的Cookie有严格的域名和安全限制,可能无法通过这种方式完全跳过验证,因此方案1更可靠。
内容的提问来源于stack exchange,提问作者منى
相关产品推荐
相关产品推荐

