NoSuchElementException 在特定网页上使用无头铬和硒

时间:2021-03-19 21:24:39

标签: java selenium

我正在尝试将无头铬用于硒并使用以下参数:

```         chromeOptions.addArguments("--window-size=1920,1080");
        chromeOptions.addArguments("--disable-extensions");
        chromeOptions.addArguments("--proxy-server='direct://'");
        chromeOptions.addArguments("--start-maximized");
        chromeOptions.addArguments("--proxy-bypass-list=*");
        chromeOptions.addArguments("--headless");
        chromeOptions.addArguments("--disable-gpu");
        chromeOptions.addArguments("--disable-dev-shm-usage");
        chromeOptions.addArguments("--no-sandbox");
        chromeOptions.addArguments("--ignore-certificate-errors");
```

我配置了 5 个不同的 webside,并且在最后一个在无头模式下在其中 4 个上运行良好,我得到了“NoSuchElementException”,但在无头模式下它运行良好。

在搜索之前,我试图在 2 个配置中使用等待方法:

```     public void waitForLoad(WebDriver driver) {
        ExpectedCondition<Boolean> pageLoadCondition = new
                ExpectedCondition<Boolean>() {
                    public Boolean apply(WebDriver driver) {
                        return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
                    }
                };
        WebDriverWait wait = new WebDriverWait(driver, 30);
        wait.until(pageLoadCondition);
    }```

第二个

```         WebDriverWait wait = new WebDriverWait(driver, 30);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@class='m-search_input js-search-input js-search_input']")));
```

我遇到了错误

``` final WebElement placeToWrite = driver.findElement(By.xpath("//input[@class='m-search_input js-search-input js-search_input']"));
```

在此网页的所有代码下方

        URL = "https://mediamarkt.pl/";
        driver.get(URL);
        System.out.println(URL + value);
        waitForLoad(driver);
        WebDriverWait wait = new WebDriverWait(driver, 30);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@class='m-search_input js-search-input js-search_input']")));
        final WebElement placeToWrite = driver.findElement(By.xpath("//input[@class='m-search_input js-search-input js-search_input']"));
        placeToWrite.sendKeys(value);
        final WebElement search = driver.findElement(By.xpath("//button[@id='js-triggerSearch']"));
        search.click();
        driver.get(driver.getCurrentUrl());
        waitForLoad(driver);
        final WebElement products = driver.findElement(By.xpath("//div[@class='b-row clearfix2 b-listing_classic js-eqContainer js-offerBox js-equalHRow']"));
        final List<WebElement> prices = products.findElements(By.xpath("//div[@itemprop='price']"));
        final List<WebElement> names = products.findElements(By.xpath("//a[@class='b-ofr_headDataTitle']"));
        System.out.println(names.size());
        System.out.println(prices.size());
        for (int i = 0; i < prices.size(); i++) {
            System.out.println(i + ".  " + names.get(i).getText() + " " + prices.get(i).getText());
        }
    }

我应该提到,比起无头模式,我不必使用任何“等待”方法。

0 个答案:

没有答案
相关问题