我正在使用java编写Selenium代码,功能如下。
polices
并搜索。然后
目前问题是,有时它会搜索到第20页,有时会搜索到第7页。直到最后都没有办法。在它之间抛出一些错误如下。
线程“main”中的异常org.openqa.selenium.WebDriverException: 未知错误:元素...无法点击 点(358,214)。其他元素将收到点击:(会话信息: chrome = 61.0.3163.100)(驾驶信息:chromedriver = 2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a),platform = Windows NT 10.0.15063 x86_64)(警告:服务器未提供任何堆栈跟踪信息)命令持续时间或超时:0毫秒构建信息: 版本:'3.5.3',修订版:'a88d25fe6b',时间: '2017-08-29T12:42:44.417Z'系统信息:主持人:'HDC3-L-6441MKV',ip: '10 .196.224.96',os.name:'Windows 10',os.arch:'amd64',os.version: '10 .0',java.version:'1.8.0_111'驱动程序信息: org.openqa.selenium.chrome.ChromeDriver功能 [{mobileEmulationEnabled = false,hasTouchScreen = false,platform = XP, acceptSslCerts = true,webStorageEnabled = true,browserName = chrome, takesScreenshot = true,javascriptEnabled = true,platformName = XP, setWindowRect = true,unexpectedAlertBehaviour =, applicationCacheEnabled = false,rotate = false, networkConnectionEnabled =假, 铬= {chromedriverVersion = 2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a), userDataDir = C:/Users/rakesh.keerthi/AppData/Local/Google/Chrome/User 数据},takesHeapSnapshot = true,pageLoadStrategy = normal, unhandledPromptBehavior =,databaseEnabled = false,handlesAlerts = true, version = 61.0.3163.100,browserConnectionEnabled = false, nativeEvents = true,locationContextEnabled = true, cssSelectorsEnabled = true}]会话ID: 72dc7f70f0d15b842c217058582a10ec at sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法) at sun.reflect.NativeConstructorAccessorImpl.newInstance(未知 来源)at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(未知 来自java.lang.reflect.Constructor.newInstance(未知来源) 在 org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:215) 在 org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:167) 在 org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40) 在 org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:82) 在 org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:45) 在 org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164) 在 org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82) 在 org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:646) 在 org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:275) 在 org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:82) 在OpenGoogleInFF.printResult(OpenGoogleInFF.java:48)at OpenGoogleInFF.main(OpenGoogleInFF.java:36)
这是我的代码。
import java.util.Date;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class OpenGoogleInFF {
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\myUserName\\Downloads\\Selenium\\chromedriver.exe");
String user = System.getProperty("user.name");
ChromeOptions options = new ChromeOptions();
options.addArguments("--user-data-dir=C:/Users/" + user + "/AppData/Local/Google/Chrome/User Data");
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.get("myUrl");
Thread.sleep(6000L);
driver.findElement(By.id("SearchText")).sendKeys("policies");
driver.findElement(By.className("ion-ios-search")).click();
Thread.sleep(5000L);
int noOfPages = driver.findElements(By.xpath(".//*[@class='pagination']/li")).size() - 5;
System.out.println("no of pages are " + noOfPages);
System.out.println(new Date());
for (int i = 0; i < noOfPages; i++) {
Thread.sleep(3000L);
List<WebElement> numberOfChildren = driver.findElements(By.xpath("//div[@ng-repeat='result in results']"));
int count = numberOfChildren.size();
printResult(driver, count);
driver.findElement(By.xpath(".//*[@id='pagingControl']/ul/li[128]/a")).click();
}
System.out.println(new Date());
}
private static void printResult(WebDriver driver, int count) throws Exception {
for (int i = 1; i <= count; i++) {
WebElement element = (new WebDriverWait(driver, 10)).until(ExpectedConditions.visibilityOfElementLocated(
By.xpath("html/body/div[1]/div/div[2]/div/div/div[2]/div[2]/div[1]/div/div/div[" + i
+ "]/ul/span[2]/div/li[1]/h3/a")));
scrollToElementByOffset(driver, element, -200).click();
Thread.sleep(3000L);
(new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(
By.xpath("html/body/div[1]/div/div[2]/div/div/section/section/div/div[4]/a[2]"))).click();
String policyNumber = driver
.findElement(By
.xpath("html/body/div[1]/div/div[2]/div/div/section/section/div/div[6]/div[2]/table/tbody/tr[1]/td[2]"))
.getText();
String policyStatement = driver.findElement(By.xpath(".//*[@id='policy-view']/section/div/div[3]/h1"))
.getText();
String policyDetails = driver
.findElement(By
.xpath("html/body/div[1]/div/div[2]/div/div/section/section/div/div[8]/div/div/div[2]/div[2]/div"))
.getText();
System.out.println(policyNumber + "\t" + policyStatement);
System.out.println("\n\n" + policyDetails + "\n\n");
driver.navigate().back();
driver.navigate().back();
}
}
private static WebElement scrollToElementByOffset(WebDriver driver, WebElement element, int offset) {
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("window.scrollTo(" + element.getLocation().getX() + ","
+ (element.getLocation().getY() + offset) + ");");
return element;
}
}
替换实际网址的道歉。这是我们组织的内部网站。
请让我知道如何优化/改进我的代码以获取所有页面的结果,以及如何减少总运行时间。
由于
答案 0 :(得分:0)
几天前,当我点击并转到下一页时,我的工作方式类似于类似项目但有时网络或浏览器问题我遇到了同样类型的问题。然后我按try
和catch
处理它...如果抛出任何错误我只是通过消息处理它而不是终止程序。如果同时发生7错误,那么我终止循环否则它将运行..
int counts=0;
try {
//Here I write a code and execute
//Program END
counts=0; //if my program end then I consider all the operation successfully and assign value count=0
}
catch (org.openqa.selenium.WebDriverException e){
count++;
System.out.println(count);
if(count>=7){
//here if sequencially 7 time i get same error then i terminate current loop and send to next loop
break;
}
使用这种方式你可以解决你的问题。希望它会帮助你..