0

无法从下拉列表中选择最大值。我正在使用 Flipkart 并尝试查找按新排序的特定范围内的前几款产品。最后,我需要将结果打印在 excel 文件中。还有一个滑块可以调整价格。主要错误行 if(driver.findElement(By.xpath("//h1[contains(text(),'5G')]")).isDisplayed())goToPhones(); 似乎与该功能无关。

//imports
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
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;

public class FlipkartSearch {

public static WebDriver driver;
    
        public static void createDriver() {
              //close the poppup for login and continue with selections.
System.setProperty("webdriver.chrome.driver","C:\\Users\\hp\\Desktop\\selenium\\qwertyuiop\\chromedriver.exe");
                  driver=new ChromeDriver();
                 driver.manage().window().maximize();
                 driver.get("https://www.flipkart.com/");
                 driver.manage().timeouts().pageLoadTimeout(5,TimeUnit.SECONDS);
        
            if(driver.findElement(By.xpath("/html[1]/body[1]/div[2]/div[1]/div[1]/button[1]")).isDisplayed())
            {
                driver.findElement(By.xpath("/html[1]/body[1]/div[2]/div[1]/div[1]/button[1]")).click();
            }
            
        }
        
        public static void goToPhones() {//get to the page for mobile phones
            
            WebElement Electronics=driver.findElement(By.xpath("//span[contains(text(),'Electronics')]"));
            
            Actions hover=new Actions(driver);
            hover.moveToElement(Electronics).build().perform();
            
            WebDriverWait wait=new WebDriverWait(driver,10);
            
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[contains(text(),'Mobiles')]")));
        driver.findElement(By.xpath("//a[contains(text(),'Mobiles')]")).click();
        driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
            
        if(driver.findElement(By.xpath("//h1[contains(text(),'5G')]")).isDisplayed())
        {
// a pop up page appears a few times, in order to ignore that and run the code again
            driver.navigate().back();
            goToPhones();
            }
        
        }
        
        public static void phoneFilters()  {// set price range filters and sort by new and store top 5 results in excel
            WebDriverWait wait=new WebDriverWait(driver,10);
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//body//div[@id='container']//div//div//div[3]//select[1]")));
            driver.findElement(By.xpath("//body//div[@id='container']//div//div//div[3]//select[1]")).click();
            Select s=new Select(driver.findElement(By.xpath("//body//div[@id='container']//div//div//div[3]//select[1]")));
            s.selectByVisibleText("₹2000");
        }
    public static void main(String[]args){
       // FlipkartSearch fs=new FlipkartSearch();
       createDriver();
        goToPhones();
        phoneFilters();
        }
     }

错误的完整列表

Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 85.0.4183.102, chrome: {chromedriverVersion: 85.0.4183.87 (cd6713ebf92fa..., userDataDir: C:\Users\hp\AppData\Local\T...}, goog:chromeOptions: {debuggerAddress: localhost:53215}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:virtualAuthenticators: true}
Session ID: 228126d9615990fe197ccf525c9635b8
*** Element info: {Using=xpath, value=//h1[contains(text(),'5G')]}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:428)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
    at FlipkartSearch.goToPhones(FlipkartSearch.java:44)
    at FlipkartSearch.main(FlipkartSearch.java:65)
4

1 回答 1

0

用工作代码替换下面的代码。

    if(driver.findElement(By.xpath("//h1[contains(text(),'5G')]")).isDisplayed()){
            // a pop up page appears a few times, in order to ignore that and run the code again
            driver.navigate().back();
            goToPhones();
   }

工作代码。

if (driver.findElements(By.xpath("//h1[contains(text(),'5G')]")).size()>0) {
        // a pop up page appears a few times, in order to ignore that and run the code again
        driver.navigate().back();
        goToPhones();
    }
于 2020-09-14T09:33:56.723 回答