junit 如何让Selenium webdriver使用存储在浏览器中的cookie来自动编写测试Tinder的脚本?

gblwokeq  于 2023-06-29  发布在  其他
关注(0)|答案(1)|浏览(109)

我正在写一个Selenium脚本,它应该自动登录到Tinder网站并执行一些操作。问题是,我希望webdriver脚本使用我的浏览器中使用的cookie,这是目前没有发生。例如,我希望Selenium脚本使用Facebook和自动存储在浏览器中的Facebook凭据登录Tinder,但这不起作用。我当前的脚本点击登录Facebook并提示我输入我的电子邮件和密码,然后导致一个新的页面,我应该输入一个发送到我手机的认证代码。我想避免所有这些麻烦,并使用存储在浏览器中的cookie。我怎么能这样做呢?

下面是我的代码:

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();
  }
}
e1xvtsh3

e1xvtsh31#

Cookies可以通过加密来使用,如下所示:

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class addCookie {
    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        try {
            driver.get("http://www.example.com");

            // Adds the cookie into current browser context
            driver.manage().addCookie(new Cookie("key", "value"));
        } finally {
            driver.quit();
        }
    }
}

相关问题