簡體   English   中英

如何使用Selenium和Java將控件從1個Chrome瀏覽器切換到另一個

[英]how to switch the control from 1 chrome browser to another using selenium with java

我正在使用https://www.myntra.com/網站進行Java硒自動使用。 單擊登錄后,它將顯示我在使用Google登錄后使用Facebook和Google進行選項登錄的信息。 我如何使用selenium來控制Google登錄頁面(如何使用selenium從https://www.myntra.com窗口將控制權切換到google登錄窗口),它們在同一瀏覽器中

要使用硒將控件切換到新窗口,實現如下

 String mainWindowHandler = driver.getWindowHandle();

    for(String winHandle : driver.getWindowHandles()){
            driver.switchTo().window(winHandle);
    }

請讓我知道您的反饋

String parentHandle = browser.getWindowHandle();
browser.findElement( By.cssSelector(".desktop-userIconsContainer") ).click();
browser.findElement( By.linkText("LOGIN") ).click();
browser.findElement( By.id("gPlusLogin") ).click();

for (String googleHandle : browser.getWindowHandles() ){
        browser.switchTo().window(googleHandle);
}

new WebDriverWait(browser, 10)
    .until(ExpectedConditions.elementToBeClickable( By.id( "identifierId" ) )).sendKeys("yoyo");
browser.switchTo().window(parentHandle);  // use this for switching back to parent window

在鏈接GOOGLEclick() ,將打開一個新Window 因此,您必須通過WindowHandles更改Window ,如下所示:

String first_win = driver.getWindowHandle();
driver.findElement(By.xpath("your_xpath")).click();
new WebDriverWait(driver,5).until(ExpectedConditions.numberOfWindowsToBe(2));
Set<String> s1 = driver.getWindowHandles();
Iterator<String> i1 = s1.iterator();
while(i1.hasNext())
{
    String next_win = i1.next();
    if (!first_win.equalsIgnoreCase(next_win))
    {
    driver.switchTo().window(next_win);

    System.out.println("Working on Sign In Window");
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM