我写了一个窗口处理函数,但它在 selenium webdriver 中不起作用 [英] I wrote a function for window handling but it's not working in selenium webdriver

查看:22
本文介绍了我写了一个窗口处理函数,但它在 selenium webdriver 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 Selenium webdriver,并且我编写了一个用于窗口处理的函数.我已经为 naukri.com 弹出处理编写了代码.我的方案是打开 naukri.com 而不关闭弹出窗口.我想切换主窗口并单击登录按钮.我已经编写了代码并创建了一个函数.当我运行脚本时,焦点在主页上并且 url 显示为已选择,但我无法单击登录按钮.我不明白问题出在哪里.请给我建议.

I am working on Selenium webdriver and I have write a function for window handling. I have written code for naukri.com popup handling. My scenario is to Open the naukri.com and without closing popup window. I want to switch main window and click on Login button.I have written the code and created a function. when I am running the script focus is going on main page and url is displayed as selected but I am not able to click on Login button. I am not understanding where the problem is.Please suggest me.

public static WebDriver fn_SetFocus_According_Title(WebDriver dObj, String arg_title)
    {
    Set<String> setcol_windowHandle=dObj.getWindowHandles();
    Iterator<String>itcol_handleval=setcol_windowHandle.iterator();

    while(itcol_handleval.hasNext()==true){
    String windowhanldval=itcol_handleval.next();
    dObj=dObj.switchTo().window(windowhanldval);
    String apptitle=dObj.getTitle();
        if(apptitle.contains(arg_title))
            {
            dObj=dObj.switchTo().window(arg_title);
            }
        }
      return dObj;
   }
}
WebDriver dObj = new FirefoxDriver();
    dObj.manage().window().maximize();
    dObj.get("https://www.naukri.com");
    dObj.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
    dObj=fn_SetFocus_According_Title(dObj,"Jobs - Recruitment - Job Search - Employment - Job Vacancies - Naukri.com");
    dObj.findElement(By.xpath("//a[@id='login_Layer']")).click();

推荐答案

在您的代码中进行以下更改:

Make the following changes in your code:

  1. 更改为:dObj.switchTo().window(windowhanldval);

  1. Change to: dObj.switchTo().window(windowhanldval);

减少arg_title"的实际字符串,因为您将在 getTitle() 获得的字符串中查找整个字符串

Reduce the actual string of "arg_title" as you would be looking for this entire string within the String obtained by getTitle()

当您已经在该页面上尝试匹配页面标题时,这意味着焦点已经在我们需要定位登录按钮元素的实际页面上.因此,完全删除第二个 switch () 行.而是使用break"来出现 if 循环.

When you are already on that page trying to match the page title which means the focus is already on the actual page where we need to locate the Login button element. So remove the second switch () line entirely. Rather use "break" to come out if loop.

让我知道这些步骤是否适合您.

Let me know if these steps works for you.

这篇关于我写了一个窗口处理函数,但它在 selenium webdriver 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆