2

So I'm learning Selenium for test automation with Java, and I have an error message like in the title, "window()" in IntelliJ is red .

I was trying to import org.openqa.selenium.WebDriver.Options, but its grayed out, so useless.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebDriver.Options;  //this one is grayed out

public class WindowsActivities {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "resources/chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
    }
}

What can I do to fix this?

Thanks in advance

Weronika
  • 29
  • 5

2 Answers2

2

It looks like you are missing this dependency (which also provides selenium-api):

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.2.1</version>
        </dependency>

Make sure you also have this dependency:

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-chrome-driver</artifactId>
            <version>4.2.1</version>
        </dependency>

Reload the project to update the dependencies.

proof of work

Please also try File | Invalidate Caches | Invalidate and Restart.

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
  • Thank You CrazyCoder. Unfortunately, after adding this dependency, it still doesn't work for me. – Weronika Jul 26 '22 at 16:51
  • Please provide the [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – CrazyCoder Jul 26 '22 at 16:55
  • I added all dependencies You've suggested, reloaded the project, Invalidated and restarted and it still doesn't work. – Weronika Jul 26 '22 at 17:05
  • 1
    Try removing the `test` scope from `pom.xml` if your code is not inside the test source roots. – CrazyCoder Jul 26 '22 at 17:06
  • OK. Thank You. Your last suggestion helped me to fix the problem. I copied the whole code into a different folder in the project. Instead of creating the class in src>test>java I created one in src>main>java. And now everything works. But hell, I have no idea why. Thank You so much. – Weronika Jul 26 '22 at 17:27
  • The difference between main and test directories can lie in place where you add your dependencies, only for test directory or for main or for whole project – cheparsky Jul 26 '22 at 18:01
  • I thought that when I add a dependency, I add it to the whole project... So, how do I check if I added dependencies only to test, main or to the whole project? Cause it seems I totally don't understand now how my project structure works. – Weronika Jul 26 '22 at 19:24
  • @Weronika Please see https://www.baeldung.com/maven-dependency-scopes. – CrazyCoder Jul 26 '22 at 19:26
  • Thank You so much, @CrazyCoder. That explained a lot. – Weronika Jul 29 '22 at 19:47
-1
  1. I recommend you to use WebDriverManager, that carries out the management of the drivers required by Selenium WebDriver: https://github.com/bonigarcia/webdrivermanager

  2. Also I recommend you to use Selenide, build on Selenium, which has a lot more advantages, instead of pure Selenium: https://selenide.org/

It will make your work easier and faster

cheparsky
  • 514
  • 6
  • 20