如何改变窗口 selenium java?

y3bcpkx1  于 2023-01-04  发布在  Java
关注(0)|答案(4)|浏览(90)

我尝试访问html标签中的iframe。xpath不起作用。如何在selenium(java/maven)中将窗口更改为iframe?

btqmn9zl

btqmn9zl1#

首先你需要创建一个driver对象,然后你可以通过id,name和WebElement来切换窗口。然后driver对象有切换回默认www.example.com的功能window.like。

// 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 to main frame
driver.switchTo().parentFrame();
// switch back one frame
driver.switchTo().defaultContent();

内容复制自(阅读本文)selenium window change article。此处为无效信息。

bsxbgnwa

bsxbgnwa2#

1.右键单击HTML页面搜索iframe。
1.获取帧ID、名称或索引。
1.将上述参数之一传递到以下命令

driver.switchTo.Frame(" ID or Name Or index");


1.然后尝试使用您的xpath。

mspsb9vt

mspsb9vt3#

您可以使用:deiver.switchTo方法与元素定位器一起使用,您可以通过以下方法找到更多内容:https://www.guru99.com/handling-iframes-selenium.html

6mzjoqzu

6mzjoqzu4#

基本上,问题是改变窗口,我们有java内置方法。

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);

相关问题