场景:在Amazon中搜索产品,然后按价格从低到高排序。 如果您有更好的建议,请提出。 以下代码对我构成了哪些挑战: 这是我的代码: 如果您有空闲时间,我可以帮忙。在此先感谢您的时间。
更新**:我进行了以下更改,现在它始终可以运行
我想到的逻辑是-A)使用while页数浏览每页... B)将价格保存在列表中C)验证list [i]
1)页数不正确-实际计数应为5,这是我在页面上看到的内容,但程序会打印6
2)价格-该程序打印一些价格(我不知道它在打印什么)。当我尝试页面中的xpath时,它会四处移动,因此我认为它可以工作。
3)程序以异常结束Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Element <a href="/s?k=yellow+puma+shoes&s=price-asc-rank&lo=visual_grid&page=4&qid=1558648339&ref=sr_pg_3">...</a> is not clickable at point (962, 579). Other element would receive the click: <div class="a-section aok-relative s-image-tall-aspect">...</div>
public class AmazonPriceTest {
public static WebDriver driver;
//public static List<WebElement> priceAsc;
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Downloads\\chromedriver_win32\\chromedriver.exe");
driver= new ChromeDriver();
//WebDriverWait wait = new WebDriverWait(driver,30);
driver.get("https://www.amazon.com/");
driver.findElement(By.xpath("//input[@id='twotabsearchtextbox']")).sendKeys("yellow puma shoe");
driver.findElement(By.xpath("//input[@value='Go']")).click();
//Implicit wait till page loads
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
//Click on result sort option where we will select price low to high
Select sortBy=new Select(driver.findElement(By.xpath("//select[@name='s']")));
sortBy.selectByValue("price-asc-rank");
//Scroll down to find pagination
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("window.scrollBy(0,5000)", "");
//Number of Pages
List<WebElement> pagesFound=driver.findElements(By.xpath("//ul[@class='a-pagination']//li//a[contains(@href, '/s?k=yellow+puma+shoes')]"));
System.out.println("Pages found " + pagesFound.size());
//Will click on Next link until we reach the last page and keep on saving price in list
while(pagesFound.size()!=0){
driver.findElement(By.xpath("//a[contains(text(),'Next')]")).click();
List<WebElement> priceAsc=driver.findElements(By.xpath("//span[@class='a-offscreen']"));
System.out.println(priceAsc.size());
}
}
}
try{
while(pagesFound.size() >=0){
driver.findElement(By.xpath("//a[contains(text(),'Next')]")).click();
List<WebElement> priceAsc=driver.findElements(By.xpath("//span[@class='a-price']//span[@class='a-offscreen']"));
//System.out.println(priceAsc.size());
for(int i=0;i<priceAsc.size();i++){
System.out.println(priceAsc.get(i).getText());
}
}
}
catch (org.openqa.selenium.NoSuchElementException e)
{
System.exit(0);
}
}
}