Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Flipkart.com价格滑块_Java_Selenium_Selenium Webdriver_Xpath - Fatal编程技术网

Java Flipkart.com价格滑块

Java Flipkart.com价格滑块,java,selenium,selenium-webdriver,xpath,Java,Selenium,Selenium Webdriver,Xpath,在Flipkart mobile search page()上,我正在尝试设置价格过滤器(最低价格=250,最高价格=25000)。 我已经为此编写了以下代码: WebElement slider1 = driver.findElement(By.xpath("//div[@class='_3G9WVX _2N3EuE']")); WebElement slider2 = driver.findElement(By.xpath("//div[@class='_3G9WVX

在Flipkart mobile search page()上,我正在尝试设置价格过滤器(最低价格=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: Unable to locate element: {"method":"xpath","selector":"//div[@class='_3G9WVX _2N3EuE']"}
我有两个问题:

  • 为什么即使xpath是正确的,它也会抛出异常
  • 如何使用滑块设置最小和最大价格
  • 以下是我的完整代码,以备您需要检查:

    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 {
    
                    }
    
        }
    
    }
    
    封装seleniumtestingscript;
    导入java.util.HashMap;
    导入java.util.Map;
    导入org.openqa.selenium.By;
    导入org.openqa.selenium.JavascriptExecutor;
    导入org.openqa.selenium.WebDriver;
    导入org.openqa.selenium.WebElement;
    导入org.openqa.selenium.chrome.ChromeDriver;
    导入org.openqa.selenium.chrome.ChromeOptions;
    导入org.openqa.selenium.interactions.Actions;
    导入org.openqa.selenium.support.ui.ExpectedConditions;
    导入org.openqa.selenium.support.ui.Select;
    导入org.openqa.selenium.support.ui.WebDriverWait;
    导入org.testng.annotations.BeforeMethod;
    导入org.testng.annotations.Test;
    //1. 敞篷车
    //2. 单击手机(快速转到>手机)
    //3. 搜索三星
    //4. 使用滑块min=250 ma=25000选择价格范围
    //5. 打印电话号码
    //6. 然后清除过滤器
    //7. 删除价格过滤器
    //8. 打印电话号码
    公共类FlipKartDemo{
    网络驱动程序;
    网络驱动程序让它等待;
    JavascriptExecutor js;
    选择;
    @预处理法
    public void launch_Browser(){
    System.setProperty(“webdriver.chrome.driver”,
    “G:\\Sheetal\\Selenium\u Program\u Practice\\Driver\\chromedriver.exe”);
    ChromeOptions选项=新的ChromeOptions();
    Map prefs=新的HashMap();
    prefs.put(“profile.managed\u default\u content\u settings.geolocation”,2);
    选项。设置实验选项(“prefs”,prefs);
    驱动程序=新的色度驱动程序(可选);
    字符串URL=”https://www.flipkart.com/";
    获取(URL);
    driver.manage().window().maximize();
    }
    @试验
    public void printPhoneName()引发InterruptedException
    {
    WebElement closeicon=driver.findElement(By.xpath(“//按钮[包含(text(),”✕')]"));
    if(closeicon.isDisplayed()){
    关闭图标。单击();
    睡眠(5000);
    动作动作=新动作(驱动);
    等待=新的WebDriverWait(驱动程序,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]”)。单击();
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(//input[@title='Search for products,brands and more']))).sendKeys(“三星”);;
    findElement(By.xpath(“//按钮[@type='submit']))。单击();
    WebElement slider1=driver.findelelement(By.xpath(“//div[@class=”\u 3G9WVX\u 2N3EuE']);
    WebElement slider2=driver.findElement(By.xpath(“//div[@class=”'u 3G9WVX oVjMho'));
    睡眠(3000);
    dragAndDropBy(滑块2,-60,0).build().perform();
    睡眠(3000);
    dragAndDropBy(滑块1,30,0).build().perform();
    }否则{
    }
    }
    }
    
    检查下面的代码,它对我有用

      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();
    
    输出: