My system is MacOS
My browser: Google Chrome 106.0.5249.119
How to check your Google Chrome version?
I put the Path in /Users/linda/Documents/testproj
https://www.selenium.dev/documentation/webdriver/getting_started/install_drivers/
I put the Path in /Users/linda/Documents/testproj
Choose the ChromeDriver version based on your chrome version!!
Choose the ChromeDriver version based on your Mac version!!
If your processor is intel, choose chromedriver_mac64.zip If your processor is Apple M1, choose chromedriver_mac_arm64.zip
How to check processor: About this Mac
How to Install Java on MacOS? https://www.geeksforgeeks.org/how-to-install-java-on-macos/
- How to set your java environment:
$ /usr/libexec/java_home -V
Java Path: /usr/local/Cellar/openjdk/18/libexec/openjdk.jdk/Contents/Home
Check you termial is bash or zsh, then choose one.
1.zsh
vi ~/.zshrc
Add: export JAVA_HOME=/usr/local/Cellar/openjdk/18/libexec/openjdk.jdk/Contents/Home
$ source ~/.zshrc
2.bash
$ open -a ~/.bash_profile
Add: export JAVA_HOME=/usr/local/Cellar/openjdk/18/libexec/openjdk.jdk/Contents/Home
$ source ~/.bash_profile
- Check the variable
$ echo $JAVA_HOME
- How to check your java version:
$ java -version
$ javac -version
- Open Command Pallete in VS Code (Ctrl / Cmd + Shift + P).
- Select: Java: Create Java Project…
- Select: No build tools
- Choose the project path, I choose /Users/linda/Documents/testproj
- Open project->Click App.java
- Click on + button on the right of the “Referenced Libraries” row present inside the Java Projects column on the sidebar.
- Select all the .jar files you downloaded on the step 2 and insert.
If it looks like this, you’re good to go.
create a new TestSelenium class:
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class TestSelenium {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "/Users/linda/Documents/testproj/chromedriver");
WebDriver driver = new ChromeDriver();
Actions actions = new Actions(driver);
driver.get("http://www.google.com");
driver.manage().window().maximize();
Thread.sleep(2000);
driver.findElement(By.name("q")).sendKeys("SFBU");
actions.sendKeys(Keys.ENTER).build().perform();
Thread.sleep(2000);
driver.navigate().to("https://sfbu.edu");
Thread.sleep(2000);
String expectedTitle = "SFBU | San Francisco Bay University | SFBU";
String actualTitle = driver.getTitle();
System.out.println("Current open web page title is " + actualTitle);
if (actualTitle.contentEquals(expectedTitle)) {
System.out.println("Test Passed!");
} else {
System.out.println("Test Failed");
}
driver.close();
driver.quit();
}
}
Go to Run and click on Run Without Debugging.
“chromedriver” cannot be opened because the developer cannot be verified.
Solution:
Step 1: find the chromedriver path. My path is: /Users/linda/Documents/testproj/chromedriver
Step 2: Now you need to tell Mac OS to trust this binary by lifting the quarantine. Do this by the following terminal command:
xattr -d com.apple.quarantine com.apple.quarantine /Users/linda/Documents/testproj/chromedriver
https://medium.com/@krsambhav/setting-up-selenium-using-java-on-vs-code-512fcb7d7279
https://funnelgarden.com/setup-selenium-with-java-on-visual-studio-code/