Java 硒。如何切换到窗口谁';的URL包含某个子字符串?
由于Selenium的操作,我打开了三个窗口。 我需要切换到窗口谁的URL包含子字符串“Servlet”Java 硒。如何切换到窗口谁';的URL包含某个子字符串?,java,selenium,Java,Selenium,由于Selenium的操作,我打开了三个窗口。 我需要切换到窗口谁的URL包含子字符串“Servlet” 如何正确地编写代码?我最近也做了类似的事情。。这里有一段摘录。(我将其从doing title移植到Url。) driver.switchTo().window(“*Servlet*”) 不幸的是,这并不是那么容易。。我们无法在那里指定通配符。。让我们为它做一些函数- /** * Waits for a window with a desired url to come u
如何正确地编写代码?我最近也做了类似的事情。。这里有一段摘录。(我将其从doing title移植到Url。)
driver.switchTo().window(“*Servlet*”)代码>
不幸的是,这并不是那么容易。。我们无法在那里指定通配符。。让我们为它做一些函数-
/**
* Waits for a window with a desired url to come up.<br>
* If it does not come up in 5 seconds, it will fail.
* <br>
* <br>
* This method will set the current active window to the window requested. If you need to switch back to the previous window, use {@link #switchToWindow(String)}
* @param regex The title of the window. Regex enabled.
* @return
*/
public void waitForWindow(String regex) {
Set<String> windows = getDriver().getWindowHandles();
for (String window : windows) {
try {
driver.switchTo().window(window);
p = Pattern.compile(regex);
m = p.matcher(driver.getCurrentUrl());
if (m.find())
return switchToWindow(regex);
} catch(NoSuchWindowException e) {
if (attempts <= 5) {
attempts++;
Thread.sleep(1);
return waitForWindow(regex);
} else {
fail("Window with url: " + regex + " did not appear after 5 tries. Exiting.");
// found
}
}
}
// when we reach this point, that means no window exists with that title..
if (attempts == 5) {
fail("Window with title: " + regex + " did not appear after 5 tries. Exiting.");
return (T) this;
} else {
System.out.println("#waitForWindow() : Window doesn't exist yet. [" + regex + "] Trying again. " + attempts + "/5");
attempts++;
return waitForWindow(regex);
}
}
使用PartOfUrl(字符串平台)的受保护无效切换选项卡{
字符串currentHandle=null;
试一试{
最终设置句柄=driver.getWindowHandles();
if(handles.size()>1){
currentHandle=driver.getWindowHandle();
}
if(currentHandle!=null){
for(最终字符串句柄:句柄){
driver.switchTo().窗口(手柄);
if(currentUrl()包含(平台)和&!currentHandle.equals(句柄)){
打破
}
}
}否则{
for(最终字符串句柄:句柄){
driver.switchTo().窗口(手柄);
如果(currentUrl()包含(平台)){
打破
}
}
}
}捕获(例外e){
System.out.println(“切换选项卡失败”);
}
}
只需调用此方法并传递要切换到的选项卡的url子字符串,如果您不完全是一个模式,您只需执行If(driver.getCurrentUrl().contains(“Servlet”){…}
。当然不是,但我强烈建议使用regex。为您提供了很多灵活性此函数是伪ey,您需要对其进行移植以满足您的需要。这将不会工作,如果你复制它,但它相当接近谢谢你的答复。但是,方法getDriver()
似乎不存在。我需要进口一些罐子吗?仔细看看上面的评论-这是我自己的推动。你必须把它移植到你需要的地方getDriver()
显然等同于driver
/**
* Waits for a window with a desired url to come up.<br>
* If it does not come up in 5 seconds, it will fail.
* <br>
* <br>
* This method will set the current active window to the window requested. If you need to switch back to the previous window, use {@link #switchToWindow(String)}
* @param regex The title of the window. Regex enabled.
* @return
*/
public void waitForWindow(String regex) {
Set<String> windows = getDriver().getWindowHandles();
for (String window : windows) {
try {
driver.switchTo().window(window);
p = Pattern.compile(regex);
m = p.matcher(driver.getCurrentUrl());
if (m.find())
return switchToWindow(regex);
} catch(NoSuchWindowException e) {
if (attempts <= 5) {
attempts++;
Thread.sleep(1);
return waitForWindow(regex);
} else {
fail("Window with url: " + regex + " did not appear after 5 tries. Exiting.");
// found
}
}
}
// when we reach this point, that means no window exists with that title..
if (attempts == 5) {
fail("Window with title: " + regex + " did not appear after 5 tries. Exiting.");
return (T) this;
} else {
System.out.println("#waitForWindow() : Window doesn't exist yet. [" + regex + "] Trying again. " + attempts + "/5");
attempts++;
return waitForWindow(regex);
}
}
/**
* Switches the current window based on title.
* @param regex A regular expression window title.
* @return
*/
public void switchToWindow(String regex) {
Set<String> windows = driver.getWindowHandles();
for (String window : windows) {
getDriver().switchTo().window(window);
System.out.println("#switchToWindow() : url=" + driver.getCurrentUrl());
p = Pattern.compile(regex);
m = p.matcher(driver.getCurrentUrl());
if (m.find())
// found
}
fail("Couldn't switch to window with urlregex: " + regex + "!");
}
waitForWindow(".*Servlet.*");
switchToWindow(".*Servlet.*");
protected void switchTabsUsingPartOfUrl(String platform) {
String currentHandle = null;
try {
final Set<String> handles = driver.getWindowHandles();
if (handles.size() > 1) {
currentHandle = driver.getWindowHandle();
}
if (currentHandle != null) {
for (final String handle : handles) {
driver.switchTo().window(handle);
if (currentUrl().contains(platform) && !currentHandle.equals(handle)) {
break;
}
}
} else {
for (final String handle : handles) {
driver.switchTo().window(handle);
if (currentUrl().contains(platform)) {
break;
}
}
}
} catch (Exception e) {
System.out.println("Switching tabs failed");
}
}