我试图在单击搜索按钮后获取页面源代码。这是我的代码:
WebDriver driver = new ChromeDriver();
driver.get(page);
WebElement el = driver.findElement(By.xpath(xpath)); // button
el.click();
driver.getPageSource();
并且此代码返回第一页的页面源,而不是单击后加载的页面......
最佳答案
您需要在获取页面源之前显式等待新页面加载。这通常取决于您正在使用的网页。例如,您可以等到特定元素变得可见:
WebDriverWait wait = new WebDriverWait(webDriver, 5);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("myid")));
driver.getPageSource();
关于java - 如何使用selenium单击按钮后获取页面源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31492363/