[英]How to get ajax added contents on web page using selenium webdriver?
I have written a code to scrap some information from one of the website. 我写了一段代码,从其中一个网站上删除一些信息。 For scrapping purpose i am using
Selenium Webdriver
. 为了报废,我使用
Selenium Webdriver
。 Now my problem is, there are some information present on one the page but initially only ten entries
are displayed on page, i am able to scrap this ten entries. 现在我的问题是,页面上存在一些信息,但是最初页面上仅显示
ten entries
,我能够删除这十个条目。 But there are some more entries which will be displayed only after clicking load more
link which is at the bottom of the page
. 但是,还有更多条目只有在单击
the bottom of the page
load more
链接后才会显示。 whenever i click load more link
remaining entries will be loaded on to the page with out refreshing
the whole page( probably ajax update
) i am unable to scrap this newly loaded entries. 每当我单击“
load more link
其余条目将被加载到该页面上with out refreshing
整个页面( probably ajax update
),我无法with out refreshing
此新加载的条目。 Please help 请帮忙
This is the code i have written 这是我写的代码
WebDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_3_6);
driver.get("some site url");
Thread.sleep(500);
driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("user name");
driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("password");
driver.findElement(By.xpath("//*[@id=\"login\"]//div/button")).click();
Thread.sleep(200);
if(driver.getPageSource().toString().contains("Hi "+un)) {
driver.get("http://www.somesite/m/searches/Loads/new");
driver.findElement(By.xpath("//*[@id=\"Criteria_PostingAge\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"Criteria_PostingAge\"]")).sendKeys("12");
driver.findElement(By.xpath("//*[@id=\"Criteria_Origin_RawValue\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"Criteria_Origin_RawValue\"]")).sendKeys(orgn[paraCount]);
driver.findElement(By.xpath("//*[@id=\"Criteria_Destination_RawValue\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"Criteria_Destination_RawValue\"]")).sendKeys(destn[paraCount]);
date=new Date();
calendar=Calendar.getInstance();
calendar.setTime(date);
driver.findElement(By.xpath("//*[@id=\"Criteria_PickupFrom\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"Criteria_PickupFrom\"]")).sendKeys(pickupDtFmt.format(date).toString());
calendar.add(Calendar.DAY_OF_MONTH, 1);
date=calendar.getTime();
driver.findElement(By.xpath("//*[@id=\"Criteria_PickupTo\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"Criteria_PickupTo\"]")).sendKeys(pickupDtFmt.format(date).toString());
driver.findElements(By.xpath("//*[@id=\"search-entry\"]//div/input")).get(11).click();
Thread.sleep(2000);
// The code upto here will get me starting ten entries
// In order to get more entries i need to click on load more o link
driver.findElement(By.xpath("//*[@id=\"loadMore\"]")).click();
Thread.sleep(3000);
WebElement myDynamicElement = (new WebDriverWait(driver, 30))
.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"search-results\"]/div[2]/ul/li[12]")));
Actually after clicking on load more
anchor link i was supposed to get ten more entries. 实际上,在点击
load more
锚链接之后,我应该再获得十个条目。 So totally 20 entries. 因此共有20个条目。 But i am getting only same 10 entries which i got when page loaded for the first time
但是我只得到第一次加载页面时得到的相同的10个条目
I bet you would get a different result if you switched to using GhostDriver instead. 我敢打赌,如果改用GhostDriver,您会得到不同的结果。 No reason to use HtmlUnitDriver when GhostDriver can do the same and is probably more cutting edge technology .
当GhostDriver可以使用HtmlUnitDriver 时,没有理由使用HtmlUnitDriver ,这可能是更先进的技术 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.