如何更改窗口的Selenium Java?

huangapple go评论85阅读模式
英文:

How to change window selenium java?

问题

如何在Selenium(Java/Maven)中将窗口切换到iframe,因为xpath不起作用?

英文:

I'm trying to access iframe inside html tag.xpath is not working.How to change my window to iframe in selenium(java/maven)?

答案1

得分: 1

首先,您需要创建驱动程序对象,然后可以通过id、名称和WebElement切换窗口。然后,驱动程序对象具有用于切换回默认窗口的函数,就像这个示例一样。

// 创建驱动程序对象
WebDriver driver = DriverManager.getDriver();

// 使用iframe的id或名称切换窗口
driver.switchTo().frame("frame id or frame name");

// 使用WebElement对象切换窗口
driver.switchTo().frame(webElement);

切换回主窗口:

// 切换回主框架
driver.switchTo().parentFrame();
// 切换回默认内容
driver.switchTo().defaultContent();

本文内容摘自这篇文章,其中包含重要信息。

英文:

First you need to create driver object and after you can switch windows by id, name and WebElement.Then driver object has functions for switch back to default window.like this example.

// create driver object
WebDriver driver = DriverManager.getDriver();

// change window using iframe id or iframe name
driver.switchTo().frame("frame id or frame name");

// change window using WebElement object
driver.switchTo().frame(webElement);

Switch back window

// switch back to main frame
driver.switchTo().parentFrame();
// switch back one frame
driver.switchTo().defaultContent();

> Content copy from (read this article) selenium window change article.Impotent information's here.

答案2

得分: 0

  1. 在 HTML 页面上右键单击,然后在其中搜索 iframe。
  2. 获取框架的ID、名称或索引。
  3. 将上述参数之一传递给以下命令
    driver.switchTo.Frame("ID或名称或索引");
    
  4. 然后尝试使用您的 XPath。
英文:
  1. Right Click on HTML Page the search there for iframe.
  2. Get the frame ID, Name or index.
  3. pass one of the above parameters into following command
    driver.switchTo.Frame(" ID or Name Or index");
    
  4. then try to use your xpath.

答案3

得分: 0

你可以使用deiver.switchTo方法与元素定位器一起使用,您可以在以下链接找到更多信息:https://www.guru99.com/handling-iframes-selenium.html

英文:

You can use: deiver.switchTo Method with the element locator, you can find more with the following: https://www.guru99.com/handling-iframes-selenium.html

答案4

得分: 0

  1. get.windowhandle(): 此方法用于获取当前窗口的窗口句柄

  2. get.windowhandles(): 此方法用于获取所有已打开窗口的句柄。它将所有当前活动的窗口存储在一个集合中。
    因此,如果你获取了所有窗口句柄,你可以像这样操作:

    代码:
    Set setLink = driver.get.windowhandles();
    现在你可以简单地进行索引和切换。
    例如:
    driver.get(setLink[2]);

  3. 切换的另一种方法是使用键盘按键:
    String clickl = Keys.chord(Keys.CONTROL, Keys.TAB);

    String clickl = Keys.chord(Keys.CONTROL, Keys.(窗口的索引,比如1、2、3));

    // 在新标签中打开链接,Keys.Chord字符串传递给sendKeys方法
    driver.findElement(
    By.xpath("任何xpath")).sendKeys(clickl);

英文:

Basically, the question is to change window we have java inbuild methods.

1. get.windowhandle(): This method helps to get the window handle of the current window

2. get.windowhandles():This method helps to get the handles of all the windows opened. It stores all the current active windows into set..
so if you get all the window handle you can do is example:

code:
Set<String> setLink = driver.get.windowhandles();
now you can simply do the indexing and switch.
ex.
driver.get(setLink[2]);

3. Another method to switch is using keyBoardkeys
     String clickl = Keys.chord(Keys.CONTROL,Keys.TAB);

     String clickl = Keys.chord(Keys.CONTROL,Keys.(Index of window you like 1,2,3));

      // open the link in new tab, Keys.Chord string passed to sendKeys
      driver.findElement(
      By.xpath("any xpath")).sendKeys(clickl); 

huangapple
  • 本文由 发表于 2020年8月7日 04:48:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63291533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定