Java 如何在Selenium中刷新DOM树以显示新生成的元素?
我想得到一个DOM元素,该元素在页面加载时不存在,但随后使用脚本添加。具体情况如下:Java 如何在Selenium中刷新DOM树以显示新生成的元素?,java,selenium,selenium-webdriver,dom,selenium-chromedriver,Java,Selenium,Selenium Webdriver,Dom,Selenium Chromedriver,我想得到一个DOM元素,该元素在页面加载时不存在,但随后使用脚本添加。具体情况如下: 我装载 我点击“TradingView”按钮 我在“技术指标”按钮上选择“随机RSI” (所有这些步骤都由驾驶员自己使用click()或机器人使用按键和鼠标按键来处理) 在我选择了随机RSI之后,添加了一些新的DOM元素,而没有刷新页面。我要查找的元素的类名为“pane legend item value wrap”,它们是在添加“随机RSI”后生成的 调用driver.findElements(By.clas
click()
或机器人使用按键和鼠标按键来处理)
在我选择了随机RSI之后,添加了一些新的DOM元素,而没有刷新页面。我要查找的
元素的类名为“pane legend item value wrap”
,它们是在添加“随机RSI”后生成的
调用driver.findElements(By.className(“窗格图例项值换行”)
会给出0个结果。我认为这是因为驱动程序页面源仍然是第一次加载页面后的源。有没有一种方法可以在不重新加载整个页面的情况下刷新该页面源或DOM树
我尝试了隐式和显式等待,但仍然没有成功,并出现以下错误:
线程“main”org.openqa.selenium.TimeoutException中出现异常:预期条件失败:等待by.className:窗格图例项值换行找到的任何元素的存在(以500毫秒的间隔尝试15秒)
以下是相关代码:
public class Main {
public static void main(String args[]) {
try {
Selenium selenium = new Selenium();
selenium.startChrome();
for (int i = 0; i < 30000; i++) {
try {
selenium.getChromeDriver().getTitle();
} catch (WebDriverException e) {
i = 30000;
continue;
}
System.out.println("Current Coin Value: " + selenium.getValueOfCoin() + "$ - " + "StRSI Blue: "
+ selenium.getBlueStRSI() + " - StRSI Red: " + selenium.getRedStRSI());
Thread.sleep(200);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
有没有办法刷新页面源或DOM树
再次调用
get()
,或使用navigate().refresh()
找不到元素的原因是它们位于“iframe”中。您应该首先切换到iframe上下文,然后单击。以下是一个工作示例:
ChromeOptions chromeOptions = new ChromeOptions();
ChromeDriver driver = new ChromeDriver(chromeOptions);
driver.get("https://www.binance.com/de/trade/pro/XRP_BTC");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@class='sc-1t2bpae-5 bEXbyP' and contains(text(),'TradingView')]"))).click();
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@class='sc-1t2bpae-5 bEXbyP' and contains(text(),'Technical Ind')]"))).click();
WebElement frameElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//iframe[contains(@id, \"tradingview\")]")));
driver.switchTo().frame(frameElement);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[contains(text(),'Stochastic RSI')]"))).click();
WebElement element = driver.findElement(By.className("pane-legend-item-value-wrap"));
driver.quit();
如果它在页面上可见,则它位于DOM中。听起来您可能需要添加一个等待,以使其可见。这只会刷新页面,导致相同的过程再次发生。正确。。。这回答了你最初提出的问题。。。在你编辑它以澄清之前。对。似乎我忘了具体说明,现在有点清楚了:)。不管怎样谢谢你,我觉得自己很愚蠢。。。我应该彻底检查一下网页的来源。我以后再试试。已经谢谢了,可能就是这个了。如果这对你有帮助,请告诉我
ChromeOptions chromeOptions = new ChromeOptions();
ChromeDriver driver = new ChromeDriver(chromeOptions);
driver.get("https://www.binance.com/de/trade/pro/XRP_BTC");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@class='sc-1t2bpae-5 bEXbyP' and contains(text(),'TradingView')]"))).click();
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@class='sc-1t2bpae-5 bEXbyP' and contains(text(),'Technical Ind')]"))).click();
WebElement frameElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//iframe[contains(@id, \"tradingview\")]")));
driver.switchTo().frame(frameElement);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[contains(text(),'Stochastic RSI')]"))).click();
WebElement element = driver.findElement(By.className("pane-legend-item-value-wrap"));
driver.quit();