java - Selenium Java,测试名称和价格时某些产品的 Np 价格
问题描述
我需要在搜索中获取所有产品及其价格,必须识别没有价格的产品,有人可以帮助我如何识别没有价格的产品并打印出有价格的产品吗?
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver","C:\\Users\\casa\\Desktop\\Drivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--lang=en");
ChromeDriver driver = new ChromeDriver(options);
driver.manage().window().maximize();
driver.get("https://www.amazon.com");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
String expetedResultSrh = "1 a 48 de más de 100,000 resultados para \"phone case\"";
Thread.sleep(4000);
driver.findElement(By.id("twotabsearchtextbox")).sendKeys("phone case"+ Keys.ENTER);
WebElement valsch = driver.findElement(By.xpath("//*[@id=\"search\"]/span/div/span/h1/div/div[1]/div/div"));
if (valsch.getText().contains("phone case")) {
System.out.println("1. "+"Result from search ::::"+ valsch.getText()+ "......PASSED");
}else {
System.out.println("Expected result failed.....UI shows Result: "+ valsch.getText()+".....FAILED");
}
//leftPanecat = Left Pane Category for Material
WebElement leftPanecat = driver.findElement(By.xpath("//*[@id=\"p_n_feature_ten_browse-bin-title\"]/span"));
System.out.println("\n"+"2. "+leftPanecat.getText());
System.out.println("\n"+"3. Clicking... See More Options"+"....PASSED");
driver.findElement(By.xpath("//*[@id=\"filters\"]/ul[4]/li[11]/span/div/a/span")).click();
Thread.sleep(2000);
//check box Carbon Fiber
if(!driver.findElement(By.cssSelector("#p_n_feature_ten_browse-bin\\/17731927011 > span > a > div > label > i")).isSelected()) {
driver.findElement(By.cssSelector("#p_n_feature_ten_browse-bin\\/17731927011 > span > a > div > label > i")).click();
}else {
System.out.println("This Box has been seleted");
}
//print label
System.out.println("\n"+"4. "+"Sucessfully Selected Material : "+driver.findElement(By.cssSelector("#p_n_feature_ten_browse-bin\\/17731927011")).getText());
****List<WebElement> productsName = driver.findElements(By.tagName("h2"));
List<WebElement> prodPrice = driver.findElements(By.xpath("//span[@class='a-price-whole']"));
List<WebElement> prodPriceFrac = driver.findElements(By.xpath("//span[@class='a-price-fraction']"));
try {
for(int i=0;i<productsName.size();i++) {
//System.out.println("\n"+i+". "+productsName.get(i).getText());
//System.out.println(prodPrice.get(J).getText());
if(productsName.get(i).getText().equals("需要帮助?")) {
productsName.remove(i);
}
System.out.println(i+". "+productsName.get(i).getText()+"------Price:: "+prodPrice.get(i).getText()+"."+prodPriceFrac.get(i).getText());
}
}**
catch(IndexOutOfBoundsException e) {
System.out.println("Passed ");
}**
解决方案
I have directly used your product page Url below after the execution of
your script.
driver.get("https://www.amazon.com/s?
k=phone+case&rh=n%3A2407760011%2Cp_n_feature_ten_browse-
bin%3A17731927011&dc&qid=1624167494&rnid=11043306011&ref=
sr_nr_p_n_feature_t en_browse-bin_5");
List<WebElement> element = driver.findElements(By.xpath(
"//*
[@id=\"search\"]/div[1]/div/div[1]/div/span[3]/div[2]/div/div/span
/div/div/div[4]/div[2]/a/span"));
for (WebElement l : element) {
if(l.getAttribute("innerText")==null) {
System.out.println("not available");
}else {
System.out.println("available"+l.getAttribute("innerText"));
}
}
I have tried to get the innerText for the product which contains
the price, It ignores the product for whom the price is not
available, Can you check with it.
推荐阅读
- python-3.x - 如何从另一个 .py 文件导入应用程序并使用 app.config 以使用配置文件中的值?
- acumatica - 从现有模板创建表单模板
- c# - 如何将一个本地字符串作为变量传递到另一个字符串的中间?.NET C#
- java - JTable 对未显示的值进行排序
- bash - Bash 字符串变量声明
- r - R:使用R从pdf文件中提取嵌入的超链接
- android - Android Studio:AbMob Banner 不会在片段中的 webview 下方显示
- c++ - C++ 对象作为 Objective-C++ 属性
- python - 使用python在IRC中发送消息的麻烦
- xml - XML Spec 是否定义在自闭合斜杠之前有空格是非法的?