gpt4 book ai didi

java - Selenium 处理多个窗口面临的问题

转载 作者:行者123 更新时间:2023-12-01 10:34:19 25 4
gpt4 key购买 nike

我编写了以下脚本,首先我将单击 FACEBOOK 登录按钮,然后弹出的 FACEBOOK 登录页面将在单独的窗口中打开。我正在输入 ID 和密码。但我的问题是,当我单击登录按钮时,该窗口将消失,并且我的父窗口 URL 将更改。如何处理更改后的 URL。我需要对此执行一些操作。我知道我可以移动到父窗口,但问题是当我单击登录时 URL 会发生变化

private  void facebookSignIn(){

WebElement element=null;
try{
element = driver.findElement(By.xpath(".//*[@id='signin_facebook_button']/div[2]"));
String mwh=driver.getWindowHandle();
element.click();

driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

Set<String> set=driver.getWindowHandles();
Iterator<String> iterator=set.iterator();

while(iterator.hasNext())
{
String popupHandle=iterator.next().toString();
if(!popupHandle.contains(mwh))
{
driver.switchTo().window(popupHandle);

element = driver.findElement(By.xpath(".//*[@id='email']"));
element.sendKeys(account);

element = driver.findElement(By.xpath(".//*[@id='pass']"));
element.sendKeys(password);

element = driver.findElement(By.xpath(".//*[@id='u_0_2']"));
countDownLatch.countDown();
countDownLatch.await();
element.click();
System.out.println("Click Done");
//driver.switchTo().window(mwh);
}
}
start = System.currentTimeMillis();

counter++;
if(counter==1)
{
startTime=System.currentTimeMillis();
}

@SuppressWarnings("unused")
WebElement sso_logout = (new WebDriverWait(driver,30)).until(ExpectedConditions.visibilityOfElementLocated(By.id("sso_logout")));
end = System.currentTimeMillis();
endTime=System.currentTimeMillis();

try{
element = driver.findElement(By.id("sso_logout"));
element.click();
}
catch(Exception e)
{
System.out.println("Exception from SSO_Logout");
}

double temp=(double)(end-start)/1000;
latencyMap.put(Thread.currentThread().getName(),temp) ;
System.out.println(Thread.currentThread().getName()+"-->Done");
}
catch(Exception e){
countDownLatch.countDown();
totalNumberOfUsers--;
System.out.println(account);
System.out.println(e.getMessage());
}
}

最佳答案

WindowHandle 不是基于 url,因此它不会影响您。如果您需要切换回来,请执行以下操作:

String parentHandle = driver.getWindowHandle();

// switch to the new window
for (String winHandle : driver.getWindowHandles()) {
if (!winHandle.equals(parentHandle))
{
driver.switchTo().window(winHandle);
}
}

//do something with the new window
element = driver.findElement(By.xpath(".//*[@id='email']"));
element.sendKeys(account);

element = driver.findElement(By.xpath(".//*[@id='pass']"));
element.sendKeys(password);

element = driver.findElement(By.xpath(".//*[@id='u_0_2']"));
countDownLatch.countDown();
countDownLatch.await();
element.click();
System.out.println("Click Done");

// switch back to the old window
driver.switchTo().window(parentHandle);

// if the new window is closed by its on
driver.switchTo().window(driver.getWindowHandle());

WindowHandle 示例:

"CDwindow-0D7BB6F6-A7E0-4DCE-B4D0-F202E85D982D"

关于java - Selenium 处理多个窗口面临的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34870379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com