gpt4 book ai didi

java - 在 Firefox 上浏览网页上的链接时出现 WebDriver 异常 - Firefox Web 浏览器

转载 作者:行者123 更新时间:2023-12-01 09:54:14 26 4
gpt4 key购买 nike

我正在尝试单击网页上的链接。网页为http://data.worldbank.org/venue-level/HIC。使用火狐浏览器

考虑我的以下实现:

//This locates all the countries that I need to click on the page.
By countryList = By.cssSelector("a[href*='data.worldbank.org/country/']");

public void retrieveCountryData() throws Exception{
List<WebElement> countryLinks = driver.findElements(countryList);
for(int i = 0;i<4;i++){
Thread.sleep(5000);
countryLinks.get(i).click();
System.out.println("The Country is: "+ countryLinks.get(i).getText());
driver.navigate().back();
}
}

出于测试目的,我尝试遍历列表中的前 4 个国家/地区。

当我运行脚本时,它给出了以下 WebDriverException。

org.openqa.selenium.WebDriverException: Element is not clickable at point (236.5, 9.5). Other element would receive the click: <a href="http://beta.data.worldbank.org"></a>
Command duration or timeout: 87 milliseconds
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 16:57:40'
System info: host: 'DESKTOP-HQKROAN', ip: '192.168.0.11', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_73'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=46.0, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 1b28ee66-df0a-4885-b937-08dc061675ea
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:327)
at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:85)
at pageObjects.WorldBankData.retrieveCountryData(WorldBankData.java:163)
at testCases.SampleTest.getCTex(SampleTest.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:774)
at org.testng.TestRunner.run(TestRunner.java:624)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
at org.testng.SuiteRunner.run(SuiteRunner.java:261)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.run(TestNG.java:1048)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:112)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:205)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:176)

请告诉我我在这里做错了什么。您可以在您端检查网页的 HTML。谢谢

最佳答案

加载您的网站后,使用以下代码:

Thread.sleep(2000);
WebDriverWait wait = new WebDriverWait(driver, 60);

List<WebElement> countryLinks1 = driver.findElements(By.cssSelector("a[href*='/country/']"));
int size = countryLinks1.size();

for(int i = 0;i<size;i++){

List<WebElement> countryLinks = driver.findElements(By.cssSelector("a[href*='/country/']"));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a[href*='/country/']")));
Thread.sleep(1000);
WebElement elem =countryLinks.get(i);

JavascriptExecutor js = (JavascriptExecutor)driver;
//this line will scroll to element.
js.executeScript("window.scrollTo(" + elem.getLocation().x + "," +(elem.getLocation().y- 100) + ");");

System.out.println("The Country is: "+ elem.getText());
elem.click();
Thread.sleep(3000);
driver.navigate().back();


}

希望它能发挥作用。

关于java - 在 Firefox 上浏览网页上的链接时出现 WebDriver 异常 - Firefox Web 浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37367301/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com