繁体   English   中英

无法单击与JQuery关联的按钮

[英]Unable to click a button which is associated with JQuery

使用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);

您只需要更改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()

通过将xpath更改为cssSelector,可以按需要多次单击“显示更多产品”:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM