无法单击框架窗口中的按钮。请在下面找到我的代码并给出相同的解决方案

时间:2015-12-23 08:14:55

标签: selenium

请在下面找到我的代码并给出相同的解决方案。谢谢

如果我使用xpath找到元素,则在运行程序时不会单击按钮。

public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub

    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.goibibo.com/");
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.manage().window().maximize();

    driver.findElement(By.linkText("Bus")).click();
    driver.findElement(By.xpath(".//*[@id='hdr_user_signin']/span/a[1]")).click();

    // List<WebElement> list = driver.findElements(By.tagName("iframe"));
    // System.out.println(list.size());
    driver.switchTo().frame(11);

    driver.findElement(By.id("id_username")).sendKeys("ayyappan.tamilvan@hcl.com");
    driver.findElement(By.id("id_password")).sendKeys("tamilayya@2");

    driver.findElement(By.xpath("//*[@class='posRel button blue large width100 fl popBtn']")).click();

}

您要求上述代码的HTML代码:

<body>
<noscript><meta HTTP-EQUIV="refresh" content="0;url='https://www.goibibo.com/accounts/login/?next=/bus/&amp;iframe=1&amp;PageSpeed=noscript'" /><style><!--table,div,span,font,p{display:none} --></style><div style="display:block">Please click <a href="https://www.goibibo.com/accounts/login/?next=/bus/&amp;iframe=1&amp;PageSpeed=noscript">here</a> if you are not redirected within a few seconds.</div></noscript>
<div class="popIframe">
<h4 class="padB20">Signin to Goibibo</h4>
<form id="login-form" method="POST">
<div id="form_error"/>
<input type="hidden" value="3Tr9wJTL7Tpw0kiK5pQBBKPfC7iTcVs1" name="csrfmiddlewaretoken"/>
<div class="form-group">
<div class="form-group">
<input type="hidden" value="/bus/" name="next"/>
<input id="signinBtn" class="button blue large width100 fl" type="submit" value="Sign In"/>
<p class="orTxt">OR</p>
<div class="fl width100 posRel">
<div class="smallLinks">
<div class="fl width100">
</form>
</div>
<script src="//goibibo.ibcdn.com/js/v2/lib-combined-min-v2.1450940392.js" type="text/javascript"/>
<script type="text/javascript" src="//goibibo.ibcdn.com/js/v2/goibibo_b2b.1450939936.js"/>
</body>

1 个答案:

答案 0 :(得分:0)

未正确使用开关功能。请看以下内容:

iframes = driver.find_elements_by_xpath("//iframe")
driver.switch_to_frame(iframes[0])

假设您正在切换到第一个iframe。或者,您可以按名称切换。

driver.switch_to.frame(driver.find_element_by_name(name))