0

I have a site storing cookies on browser as, language = en I tries to output all the cookies by using the java code via selenium,

ChromeDriver driver = new ChromeDriver();
driver.navigate().to(url);
System.out.println(driver.manage().getCookies());

but I do not have any luck of getting the cookies that I have defined in my site. I tried using geckodriver and chromedriver, both resulting not getting the cookies key and value. How do I enable the cookies so that I can get all the defined cookies, on my site. I do know following the below link StackOverFlow disables the cookies, but how to enable it.

In addition, me accessing the site normally via chrome browser was able to read the cookies from the application storage, but in Firefox still doesn't have those cookies key/value at the storage, maybe due to default setting to not accept any cookies. Thanks for the help.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

0

I don't see any issue in your code block as such. Perhaps before collecting and printing the cookies you need to induce WebDriverWait for the visibilityOfElementLocated() for a visible element and then collect the as follows:

ChromeDriver driver = new ChromeDriver();
driver.navigate().to(url);
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("cssSelectorVisibleElement")));
System.out.println(driver.manage().getCookies());

Or induce WebDriverWait for the elementToBeClickable() for an interactive element and then collect the as follows:

ChromeDriver driver = new ChromeDriver();
driver.navigate().to(url);
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("cssSelectorClickableElement")));
System.out.println(driver.manage().getCookies());
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thank for the feedback, actually that's doesn't work, I have tried it before I post the question. I do have WebDriverWait to get some element visible before I call the getCookies() method. – Ershad Ahamed Jan 19 '21 at 17:07
  • How are you storing the cookies? I don't see you loading any specific profile even, What are you trying to do? – undetected Selenium Jan 19 '21 at 17:12
  • This below is my complete code: `System.setProperty("webdriver.chrome.driver", this.getDriverPath()); ChromeDriver driver = new ChromeDriver(); WebDriverWait wait = new WebDriverWait(driver, 20); driver.navigate().to(url); wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(load))); System.out.println(driver.manage().getCookies());` – Ershad Ahamed Jan 19 '21 at 17:17
  • Which element is that `By.cssSelector(load)`? You are spinning up a fresh browser? Why are you expecting your custom cookies? – undetected Selenium Jan 19 '21 at 17:22
  • What I have observe is, my site will automatically set language cookie upon first call to the domain. It will set for example cookies key: language and value: in. If for some reason my site not allowed to store cookie on browser, I will serve default language. By default in chrome works (will be cookies in application storage) fine but not in firefox (no cookies only session variables). So I tried to use selenium via geckodriver and chromedriver both behave the same, by not getting the cookies I set, which is language, but I can get all the session variables.Si I tought need to enable cookies – Ershad Ahamed Jan 19 '21 at 17:42
  • So do I need to create or set any profile? How do I that? Tqs for your help. – Ershad Ahamed Jan 19 '21 at 17:43
  • Of coarse you need to set up a dedicated [Chrome Profile](https://stackoverflow.com/questions/50635087/how-to-open-a-chrome-profile-through-user-data-dir-argument-of-selenium/50637211#50637211) first to store the cookies(info) and then load the same [Chrome Profile](https://stackoverflow.com/questions/49270109/how-to-open-a-chrome-profile-through-python/49280195#49280195) through [Selenium](https://stackoverflow.com/questions/54459701/what-is-selenium-and-what-is-webdriver/54482491#54482491) – undetected Selenium Jan 19 '21 at 17:58
  • I will try first on both Linux and Windows. Definitely will accept the answer if it works. – Ershad Ahamed Jan 21 '21 at 14:20