2
場景:轉到Yahoo.com搜索hotmail,然後單擊所有搜索頁面。點擊所有結果鏈接
我的代碼:它只是打印結果頁面的鏈接。有沒有辦法在沒有頁面的情況下點擊頁面2,3,4?
public class QAJob {
@Test
public void jobSearch(){
WebDriver driver= new FirefoxDriver();
driver.get("https://yahoo.com");
driver.findElement(By.id("uh-search-box")).sendKeys("hotmail");
driver.findElement(By.id("uh-search-button")).click();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
//scroll down
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("window.scrollBy(0,1000)", "");
List<WebElement> result_pages=driver.findElements(By.xpath("//div[contains(@class,'pagination')]//a[contains(@title,'')]"));
for (WebElement e: result_pages){
Actions action= new Actions(driver);
WebElement search_results=driver.findElement(By.xpath("//div[contains(@class,'pagination')]//a[contains(@title,'')]"));
System.out.println(e.getAttribute("outerHTML"));
}
}
}
你是什麼意思'一次搜索所有搜索頁面?多次點擊同時? – Andersson
我不明白你的問題,我知道你不能點擊鏈接?只需在for循環中使用以下代碼鏈接:e.click() –
我可以找到頁面2,3,4的xpath並對其進行硬編碼,而我希望能夠點擊頁面2和頁面3,然後下一頁,以便我不必對xpath進行硬編碼,同時進行多次點擊。 –