Selenium - 无法根据 [a link] http://www.cartasi.it/gtwpages/index.jsp 上的 CSS 元素找到密码字段元素

Selenium - Unable to find password field element based on CSS element on [a link] http://www.cartasi.it/gtwpages/index.jsp

我正在尝试使用 CSS 选择器从 link http://www.cartasi.it/gtwpages/index.jsp 中查找密码字段元素。以下是我用于其他网站的代码,它适用于除提供的 link 之外的所有网站。

 pwd=driver.findElement(By.cssSelector("input[type='password']")) 

并检查了网站的源代码,但我没有在代码中找到任何 type="password" 关键字。 我觉得发布整个网站的源代码会造成混乱,因此给了 link 以供参考。 是什么导致此密码被隐藏?如何使用 CSS 选择器定位元素?任何帮助将不胜感激

该页面上的密码字段位于 iframe:

<iframe width="254" height="120" frameborder="0" src="https://titolari.cartasi.it/portal/login/login.xhtml" marginheight="0" marginwidth="0" scrolling="no">
...
<input id="loginForm:password" class="ui-inputfield ui-password ui-widget ui-state-default ui-corner-all" type="password" tabindex="2" placeholder="" name="loginForm:password" role="textbox" aria-disabled="false" aria-readonly="false">

所以您需要先切换到 iframe,然后再使用您的选择器:

driver.switchTo().defaultContent(); // make sure you are on main page
driver.switchTo().frame(
    driver.findElement(By.xpath("//iframe[contains(@src, 'login.xhtml')]")));
pwd=driver.findElement(By.cssSelector("input[type='password']")) 

当然你可以改变xpath/selection方法为任何你喜欢的。