我需要点击动态加载的页面的特定元素。滚动页面时生成的Web元素。类似于jabong网页。
我尝试在jabong网页上这样做,这是我的代码
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.navigate().to("http://www.jabong.com/men/clothing/"
+ "?source=topnav");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
System.out.println("Close the modal popup");
driver.findElement(By.id("jab-vchr-cls")).click();
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
/**
* while(true) loop is required to search the
* element until element found.We put find
* element within try-catch and if it get
* exception it scroll the page and again
* try to find the element.
*/
while(true) {
((JavascriptExecutor)driver).executeScript("window.scrollBy(0,100)", "");
try {
WebElement element = driver.findElement(By.xpath("//*[@id='http: //static3.jassets.com/p/The-Indian-Garage-Co.-Checks-Red-Casual-Shirt-2889-679124-1-catalog.jpg']/img"));
Wait<WebDriver> wait_element=new WebDriverWait(driver, 10);
wait_element.until(ExpectedConditions.elementToBeClickable(element));
element.click();
System.out.println("!!!!!!!!!!!!!!At Last Get Success!!!!!!!!!!!!!!!!");
break;
}
catch (Exception ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
System.out.println(ex.getMessage());
}
}
} }
我的问题是
1.有没有更好的方法来做这件事?
2.如何让这个脚本更快?
答案 0 :(得分:2)
你可以这样做,如果你想避免while(true)虽然我不认为这个循环有任何问题。
boolean reachedbottom = Boolean.parseBoolean(js.executeScript("return $(document).height() == ($(window).height() + $(window).scrollTop());").toString());
while (!reachedbottom) {
((JavascriptExecutor) driver).executeScript("window.scrollBy(0,600)", "");
try {
reachedbottom=Boolean.parseBoolean(js.executeScript("return $(document).height() == ($(window).height() + $(window).scrollTop());").toString());
WebElement element = driver.findElement(By.xpath("//*[@id='http://static3.jassets.com/p/The-Indian-Garage-Co.-Checks-Red-Casual-Shirt-2889-679124-1-catalog.jpg']/img"));
Wait<WebDriver> wait_element = new WebDriverWait(driver, 5);
wait_element.until(ExpectedConditions.elementToBeClickable(element));
element.click();
System.out.println("!!!!!!!!!!!!!!At Last Get Success!!!!!!!!!!!!!!!!");
break;
} catch (Exception ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
System.out.println(ex.getMessage());
}
}
答案 1 :(得分:0)
在Getting Started with Selenium框架中,AutomationTest#waitForElement
方法是处理事情的好方法。它是webdriver等待的替代品,但工作原理完全相同。
/**
* Private method that acts as an arbiter of implicit timeouts of sorts.. sort of like a Wait For Ajax method.
*/
private WebElement waitForElement(By by) {
int attempts = 0;
int size = driver.findElements(by).size();
while (size == 0) {
size = driver.findElements(by).size();
if (attempts == MAX_ATTEMPTS) fail(String.format("Could not find %s after %d seconds",
by.toString(),
MAX_ATTEMPTS));
attempts++;
try {
Thread.sleep(1000); // sleep for 1 second.
} catch (Exception x) {
fail("Failed due to an exception during Thread.sleep!");
x.printStackTrace();
}
}
if (size > 1) System.err.println("WARN: There are more than 1 " + by.toString() + " 's!");
return driver.findElement(by);
}
你可以把它从无限循环中取出来。如果页面在10秒内没有加载某些东西,那么我会说这是一个需要纠正的应用程序问题。将其从无限循环中取出,并使用上面指定的waitForElement
之类的内容,或者只使用WebDriverWait
类。
此外,您不必滚动到元素。我还没有找到做这样事情的理由。只要该元素在DOM上,它就应该能够被操作。