Java - 解析动态生成的 HTML 个元素
Java - Parsing HTML elements generated dinamically
我想在生成所有动态元素(例如 Chrome 中的“检查”按钮)后解析 HTML 页面。
我做了一些研究并遇到了 Selenium API,但我仍然无法得到我想要的东西。
在 rorgente 中有以下元素“div”:
<div id="tab-match-history" style="display: none;">
<div id="match-history-preload" class="preload-panel">
<div class="preload">
<span>Loading...</span>
</div>
</div>
<div id="match-history-content"></div>
</div>
如您所见,它包含空的“match-history-content”元素。
该元素是动态创建的并变为:
<div id="match-history-content">
<div class="color-px-spacer submenu"> </div>
<div class="lines-bookmark">
<ul class="ifmenu">
<li class="divider"/>
<li id="mhistory-1-history" class="li0 selected">
<span>
<a onclick="detail_tab(['match-history', '1-history']);">Set 1</a>
</span>
</li>
<li class="divider"/>
<li id="mhistory-2-history" class="li1">
<span>
<a onclick="detail_tab(['match-history', '2-history']);">Set 2</a>
</span>
</li>
<li class="divider"/>
</ul>
</div>
<!-- . . . -->
<div id="tab-mhistory-1-history" style="display: block;">
<table id="parts" class="parts-first">
<tbody>
<!-- . . . -->
</tbody>
</table>
<div class="spacer-block"/>
</div>
<div id="tab-mhistory-2-history" style="display: block;">
<table id="parts" class="parts-first">
<tbody>
<!-- . . . -->
</tbody>
</table>
<div class="spacer-block"/>
</div>
</div>
我需要分析两个元素“tab-mhistory-1-history”和“tab-mhistory-2-历史".
问题是 WebDriver.getPageSource() 方法给了我“匹配历史内容”div 没有动态生成的元素。
<div id="match-history-content"></div>
下面是我使用的 Java 代码:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Main {
public static void main( String[] args ) {
final String URL = "https://www.flashscore.it/partita/GIecNDAM/#cronologia-dell-incontro;1";
System.setProperty("webdriver.chrome.driver","C:\dev_prog\chromedriver_win32\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get(URL);
System.out.println(driver.getPageSource());
System.out.println(driver.findElement(By.id("match-history-content")));
System.out.println(driver.findElement(By.id("mhistory-1-history"))); // This command generates the error: no such element: Unable to locate element: {"method":"css selector","selector":"#mhistory\-1\-history"}
driver.close();
driver.quit();
}
}
提前致谢
放:
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
之前:
driver.get(URL);
或代替:
driver.findElement(By.id("mhistory-1-history"))
使用:
new WebDriverWait(driver, 30).until(ExpectedConditions.elementToBeClickable(By.id("mhistory-1-history")))
我想在生成所有动态元素(例如 Chrome 中的“检查”按钮)后解析 HTML 页面。
我做了一些研究并遇到了 Selenium API,但我仍然无法得到我想要的东西。
在 rorgente 中有以下元素“div”:
<div id="tab-match-history" style="display: none;">
<div id="match-history-preload" class="preload-panel">
<div class="preload">
<span>Loading...</span>
</div>
</div>
<div id="match-history-content"></div>
</div>
如您所见,它包含空的“match-history-content”元素。 该元素是动态创建的并变为:
<div id="match-history-content">
<div class="color-px-spacer submenu"> </div>
<div class="lines-bookmark">
<ul class="ifmenu">
<li class="divider"/>
<li id="mhistory-1-history" class="li0 selected">
<span>
<a onclick="detail_tab(['match-history', '1-history']);">Set 1</a>
</span>
</li>
<li class="divider"/>
<li id="mhistory-2-history" class="li1">
<span>
<a onclick="detail_tab(['match-history', '2-history']);">Set 2</a>
</span>
</li>
<li class="divider"/>
</ul>
</div>
<!-- . . . -->
<div id="tab-mhistory-1-history" style="display: block;">
<table id="parts" class="parts-first">
<tbody>
<!-- . . . -->
</tbody>
</table>
<div class="spacer-block"/>
</div>
<div id="tab-mhistory-2-history" style="display: block;">
<table id="parts" class="parts-first">
<tbody>
<!-- . . . -->
</tbody>
</table>
<div class="spacer-block"/>
</div>
</div>
我需要分析两个元素“tab-mhistory-1-history”和“tab-mhistory-2-历史".
问题是 WebDriver.getPageSource() 方法给了我“匹配历史内容”div 没有动态生成的元素。
<div id="match-history-content"></div>
下面是我使用的 Java 代码:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Main {
public static void main( String[] args ) {
final String URL = "https://www.flashscore.it/partita/GIecNDAM/#cronologia-dell-incontro;1";
System.setProperty("webdriver.chrome.driver","C:\dev_prog\chromedriver_win32\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get(URL);
System.out.println(driver.getPageSource());
System.out.println(driver.findElement(By.id("match-history-content")));
System.out.println(driver.findElement(By.id("mhistory-1-history"))); // This command generates the error: no such element: Unable to locate element: {"method":"css selector","selector":"#mhistory\-1\-history"}
driver.close();
driver.quit();
}
}
提前致谢
放:
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
之前:
driver.get(URL);
或代替:
driver.findElement(By.id("mhistory-1-history"))
使用:
new WebDriverWait(driver, 30).until(ExpectedConditions.elementToBeClickable(By.id("mhistory-1-history")))