我的方案是搜索任何品牌笔记本电脑,我需要在当前页面中选择最高价格而不进行排序。
@BeforeClass
public void launchBrowser()
{
System.setProperty("webdriver.chrome.driver", "F:\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.flipkart.com");
}
@Test(priority = 0)
public void searchComputers() throws InterruptedException
{
WebDriverWait wait = new WebDriverWait(driver, 40);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@title='Search for Products, Brands and More']")));
driver.findElement(By.xpath("//div[@class='O8ZS_U']/input")).sendKeys("laptops");
driver.findElement(By.xpath("//div[@class='O8ZS_U']/input")).sendKeys(Keys.ENTER);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@title='HP']//div[@class='_1p7h2j']")));
Thread.sleep(1000);
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,250)", "");
driver.findElement(By.xpath("//div[@title='HP']//div[@class='_1p7h2j']")).click();
}
@Test(priority = 1)
public void searchHPComputers() throws InterruptedException
{
Thread.sleep(2000);
WebDriverWait wait1 = new WebDriverWait(driver, 40);
wait1.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//li[text()='Newest First']")));
Thread.sleep(1000);
List<WebElement> element = driver.findElements(By.xpath("//div[@class='_1vC4OE _2rQ-NK']"));
for(WebElement web : element)
{
String amount = web.getText();
int length = amount.length();
String price = amount.substring(1, length);
System.out.println("Amount : "+ price);
Thread.sleep(1000);
}
}
@AfterClass
public void closeBrowser() throws InterruptedException
{
Thread.sleep(2000);
driver.close();
driver.quit();
}
我已经在eclipse控制台的当前页面中打印了所有价目表。 但我不知道如何点击最高价格的笔记本电脑。 请帮我解决这个问题。
答案 0 :(得分:1)
如果您想以最高价格点击搜索,则需要执行以下两项操作:
#1 评论以下几行:
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,250)", "");
#2 使用以下代码替换searchHPComputers()
方法正文
Thread.sleep(5000);
driver.findElement(By.xpath("//li[text()='Price -- High to Low']")).click();
Thread.sleep(5000);
List<WebElement> element = driver.findElements(By.xpath("//div[@class='_1vC4OE _2rQ-NK']"));
element.get(0).click();
瞧!!
<强>更新强>
@Test(priority = 1)
public void searchHPComputers() throws InterruptedException
{
Thread.sleep(2000);
WebDriverWait wait1 = new WebDriverWait(driver, 40);
wait1.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//li[text()='Newest First']")));
Thread.sleep(1000);
List<WebElement> element = driver.findElements(By.xpath("//div[@class='_1vC4OE _2rQ-NK']"));
List<WebElement> elementLink = driver.findElements(By.xpath("//div[@class='_3wU53n']"));
int largestPrice =0, elementIndex =0, i=0;
for(WebElement web : element)
{
String amount = web.getText();
int length = amount.length();
String price = amount.substring(1, length);
price = price.replaceAll(",", "");
int priceInt = Integer.parseInt(price);
System.out.println("Amount : "+ priceInt);
Thread.sleep(1000);
if(priceInt > largestPrice) {
largestPrice = priceInt;
elementIndex = i;
}
i++;
}
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].scrollIntoView(true);", elementLink.get(elementIndex-1));
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(elementLink.get(elementIndex)));
elementLink.get(elementIndex).click();
}