gpt4 book ai didi

java - Flipkart.com 价格 slider xpath

转载 作者:行者123 更新时间:2023-12-02 08:40:56 25 4
gpt4 key购买 nike

在 Flipkart 移动搜索页面( https://www.flipkart.com/search?q=samsung&otracker=search&otracker1=search&marketplace=FLIPKART&as-show=on&as=off )上,我尝试设置价格过滤器(最低价格=250,最高价格=25000)。我为此编写了以下代码:

WebElement slider1 = driver.findElement(By.xpath("//div[@class='_3G9WVX _2N3EuE']"));
WebElement slider2 = driver.findElement(By.xpath("//div[@class='_3G9WVX oVjMho']"));
Thread.sleep(3000);

act.dragAndDropBy(slider2, -60, 0).build().perform();
Thread.sleep(3000);
act.dragAndDropBy(slider1, 30, 0).build().perform();

但它抛出 No such element 异常:

no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class='_3G9WVX _2N3EuE']"}

我有两个疑问:

  1. 为什么 xpath 正确却抛出异常?
  2. 如何使用 slider 设置最低和最高价格?

下面是我的完整代码,以防您需要检查:

package seleniumtestingscript;
import java.util.HashMap;
import java.util.Map;
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.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

//1. Open Flipkart
//2. Click on MObile(Go quickly to > MObiles)
//3. Search Samsung
//4. Select price range using slider min=250 ma=25000
//5. Print the phone name
//6. Then clear the filter
//7. Remove price filter
//8. Print the phone name

public class FlipKartDemo {

WebDriver driver;
WebDriverWait wait;
JavascriptExecutor js;
Select select;

@BeforeMethod
public void launch_Browser() {

System.setProperty("webdriver.chrome.driver",
"G:\\Sheetal\\Selenium_Program_Practice\\Driver\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.managed_default_content_settings.geolocation", 2);
options.setExperimentalOption("prefs", prefs);

driver = new ChromeDriver(options);

String URL = "https://www.flipkart.com/";
driver.get(URL);
driver.manage().window().maximize();

}

@Test
public void printPhoneName() throws InterruptedException
{
WebElement closeicon = driver.findElement(By.xpath("//button[contains(text(),'✕')]"));

if (closeicon.isDisplayed()) {
closeicon.click();
Thread.sleep(5000);
Actions act = new Actions(driver);
wait = new WebDriverWait(driver, 15);
WebElement Electronics = driver.findElement(By.xpath("//span[contains(text(),'Electronics')]"));
act.moveToElement(Electronics).build().perform();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(//a[@title='Mobiles'])[position()=1]"))).click();



wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@title='Search for products, brands and more']"))).sendKeys("samsung");;
driver.findElement(By.xpath("//button[@type='submit']")).click();
WebElement slider1 = driver.findElement(By.xpath("//div[@class='_3G9WVX _2N3EuE']"));
WebElement slider2 = driver.findElement(By.xpath("//div[@class='_3G9WVX oVjMho']"));
Thread.sleep(3000);

act.dragAndDropBy(slider2, -60, 0).build().perform();
Thread.sleep(3000);
act.dragAndDropBy(slider1, 30, 0).build().perform();


} else {

}

}

}

最佳答案

检查下面的代码,它对我有用。

  driver.get("https://www.flipkart.com/search?q=samsung&otracker=search&otracker1=search&marketplace=FLIPKART&as-show=on&as=off&p%5B%5D=facets.fulfilled_by%255B%255D%3DFlipkart%2BAssured&p%5B%5D=facets.price_range.from%3DMin&p%5B%5D=facets.price_range.to%3D16000");

WebElement slider1 = driver.findElement(By.xpath("//div[@class='_3G9WVX oVjMho']//div[@class='_3aQU3C']"));
WebElement slider2 = driver.findElement(By.xpath("//div[@class='_3G9WVX _2N3EuE']//div[@class='_3aQU3C']"));
Thread.sleep(3000);
Actions act = new Actions(driver);
WebDriverWait wait= new WebDriverWait(driver, 15);
act.dragAndDropBy(slider2, -60, 0).build().perform();
Thread.sleep(3000);
act.dragAndDropBy(slider1, 30, 0).build().perform();

输出:

enter image description here

关于java - Flipkart.com 价格 slider xpath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61394726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com