Selenium webdriver 不像浏览器那样解释标记,我不能像浏览器那样加载 DOM

我想使用 java webdriver selenium 在https://appleid.apple.com/中自动执行身份验证过程,但表单的 html 元素未加载到 DOM 中


据我所知,Selenium webdriver 像浏览器一样解释标记。它应用 CSS 样式,运行 JavaScript 并将动态呈现的内容添加到 DOM


为什么 HTML 元素没有加载到 DOM 中?


我该如何继续,修复它并加载 DOM 中的所有元素,就像浏览器一样?


注意:https ://appleid.apple.com/网站使用 Mustache.JS(无逻辑模板)


 public static void main(String[] args) {


    System.setProperty("webdriver.chrome.driver", "chromedriver.exe");

    ChromeOptions options = new ChromeOptions();

    options.addArguments("--headless", "--disable-gpu", "--window-size=1920,1200", "--ignore-certificate-errors");

    WebDriver driver = new ChromeDriver(options);

    driver.get("https://appleid.apple.com/");

    waitForPageLoadComplete(driver, 30);

    //can't found input name element

    WebElement inputName = driver.findElement(By.id("account_name_text_field"));

    System.out.println(driver.getPageSource());

}


慕尼黑5688855
浏览 118回答 1
1回答

慕斯709654

您要查找的元素位于 iFrame 中。您需要先切换到此 iFrame,然后继续查找已有的元素。driver.switchTo().frame("aid-auth-widget-iFrame");WebElement inputName = driver.findElement(By.id("account_name_text_field"));您可以在此处找到有关切换到 iFrame 的一些其他信息: https ://www.guru99.com/handling-iframes-selenium.html
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java