无法单击与JQuery关联的按钮

时间:2017-02-03 11:29:39

标签: java selenium web-scraping scrapy htmlunit

使用HtmlUnit / Selenium我无法点击页面http://www.myntra.com/men-tshirts中的“显示更多产品”div。在实时浏览器中单击div正在后台执行JQuery,该JQuery作为http://myntra.myntassets.com/myx/javascripts/search.min.bb3ae82fb0f65d5447b1c9aed4afbd3eac8291b6.js的一部分与页面一起加载并向服务器发布新请求。我想抓取这个网站的图片。我曾尝试使用Nutch,scrapy和crawler4j抓取工具,但他们并不单独支持按钮点击。任何人都可以帮助我知道如何通过代码处理这种点击?或者除了HtmlUnit或Selenium之外是否还有其他api可以帮助我点击这种元素?

以下是我尝试过的代码:

硒:

    WebDriver driver = new FirefoxDriver();

    driver.get("http://www.myntra.com/men-tshirts");
    WebElement loadMoreDiv = driver.findElement(By.xpath("//div[contains(text(), 'Show More Products')]"));
    loadMoreDiv.click();

    (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver d) {
            WebElement divTags = d.findElement(By.xpath("//div[contains(text(), 'Show More Products')]"));
            if(divTags != null){
                return true;
            }
            return false;
        }
    });

    String pageSource = driver.getPageSource();
    System.out.println(pageSource);
    driver.quit();

的HtmlUnit:

    WebClient client = new WebClient(BrowserVersion.CHROME);
    client.getOptions().setTimeout(30000);
    client.getOptions().setCssEnabled(true);
    client.getOptions().setJavaScriptEnabled(true);
    client.getOptions().setThrowExceptionOnFailingStatusCode(false);
    client.getOptions().setThrowExceptionOnScriptError(false);
    client.setAjaxController(new NicelyResynchronizingAjaxController());

    HtmlPage page = client.getPage("http://www.myntra.com/men-tshirts");
    client.waitForBackgroundJavaScript(30000); 
    System.out.println("******************* the page after loading is : \n" + page.asXml());        

    HtmlDivision loadMoreDiv = page.getFirstByXPath("//div[contains(text(), 'Show More Products')]");

    page = loadMoreDiv.click();

    client.waitForBackgroundJavaScript(30000);

    System.out.println("******************* the page after the clicking is : \n" + page.asXml());

解决方案:

对不起!!!实际上我的配置存在一些问题。我使用的是旧版浏览器(firefox 27)和驱动程序(FirefoxDriver 2.40.0)。更新它们(FireFox 51.0.1和FirefoxDriver 3.0.1)并将以下行添加到代码后,允许我单击所需的div

System.setProperty("webdriver.gecko.driver", "C:\\myPath\\geckodriver.exe"); 
DesiredCapabilities capabilities = DesiredCapabilities.firefox(); 
capabilities.setCapability("marionette", true); 
WebDriver driver = new MarionetteDriver(capabilities);

2 个答案:

答案 0 :(得分:0)

您只需要更改XPath作为目标按钮,最初没有样式(style=''),但首次点击style="display: block;"后,"//div [@class='show-more'][@style='']"))不再适用......试试吧

WebElement loadMoreDiv = driver.findElement(By.xpath("//div[@class='show-more']"))

WebElement loadMoreDiv = driver.findElement(By.xpath("//div[contains(text(), 'Show More Products')]"))

<强>更新

以上代码适用于Chrome,但不适用于Firefox。如果您仍想使用Firefox,可以使用以下解决方法:

WebElement loadMoreDiv = driver.findElement(By.xpath("//div[contains(text(), 'Show More Products')]"))
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView;", loadMoreDiv);
driver.findElement(By.xpath("//body")).sendKeys(Keys.PAGE_UP)
loadMoreDiv.click()

答案 1 :(得分:0)

我可以通过将xpath更改为cssSelector来点击显示更多产品,如下所示:

WebElement loadMoreDiv = driver.findElement(By.cssSelector("div.show-more"));
loadMoreDiv.click()