我相信我的 chromedriver 文件在正确的位置(模糊的's ....'让我们称这个文本为“sName”):
我不明白的是,为什么它在我的所有其他文件中都能正常工作?例如,在左侧的 Package Explorer 中,您可以看到“section4”项目,我在其中制作了文件,这些文件让我可以转到 flipkart.com 并查看他们出售的移动电源。 “section4”项目工作得很好,我相信它也依赖于相同的 chromedriver。
flipKart.java:
package section4flipkart;
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import utils.WaitUtility;
public class flipKart {
WebDriver driver = new ChromeDriver();
String url = "https://www.flipkart.com/";
public void invokeBrowser() {
System.setProperty("webdriver.chrome.driver", "/Users/sName/eclipse-workspace/libs/chromedriver.exe");
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.get(url);
}
public void mouseHover() {
driver.findElement(By.xpath("//button[@class='_2KpZ6l _2doB4z']")).click();
WebDriverWait wait1 = new WebDriverWait(driver, Duration.ofSeconds(10));
wait1.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[text()='Electronics']")));
WebElement electronicsLink = driver.findElement(By.xpath("//div[text()='Electronics']"));
Actions action = new Actions(driver);
action.moveToElement(electronicsLink).build().perform();
WaitUtility.waitTillElementVisible(driver, By.xpath("//a[text()='Powerbank']"), Duration.ofSeconds(10));
WebElement powerbankLink = driver.findElement(By.xpath("//a[text()='Powerbank']"));
action.moveToElement(powerbankLink).click().build().perform();
}
public static void main(String[] args) {
flipKart fp = new flipKart();
fp.invokeBrowser();
fp.mouseHover();
}
}
WaitUtility.java 文件:
package utils;
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class WaitUtility {
public static void waitTillElementVisible(WebDriver driver, By by, Duration timeoutInSeconds) {
WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(by));
}
}
不确定为什么这不起作用。我试过打开终端,导航到 chromedriver 文件目录并运行:
spctl --add --label 'Approved' chromedriver
但不确定这是否有所作为。尝试这个时我改变了
"/Users/sName/eclipse-workspace/libs/chromedriver.exe"
到
"/Users/sName/eclipse-workspace/libs/chromedriver"
但我的 Mac 仍然显示“无法打开“chromedriver”,因为无法验证开发人员”弹出窗口。
好的,我解决了它,感谢 chatgpt 哈哈。
这是我所做的: