【问题标题】:Selenium webdriver doesn't interpret the markup as a browser does, I can't load all HTML elements in the DOM as a browser doesSelenium webdriver 不会像浏览器那样解释标记,我不能像浏览器那样加载 DOM 中的所有 HTML 元素
【发布时间】:2019-05-27 06:11:04
【问题描述】:

我想使用 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());
}

【问题讨论】:

  • 尝试使用不同类型的选择器(例如使用值form-textbox-text 的类选择器)查找元素是否返回元素?这将有助于缩小问题的范围。我还建议使用 IDE 的调试功能逐步执行代码。

标签: java selenium xpath css-selectors webdriverwait


【解决方案1】:

您要查找的元素位于 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

【讨论】:

    猜你喜欢
    • 2023-03-05
    • 1970-01-01
    • 2015-11-04
    • 2016-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多