简体   繁体   中英

unable to locate element in new tab chrome - Selenium Webdriver with java

When clicking the menu in the application, it automatically opens that page in a new tab, then do the action in the new tab

String mainWindow = driver.getWindowHandle();

//when click the below its opened in new tab
driver.findElement(By.cssSelector("span.slds-align-middle")).click();
Set<String> handles = driver.getWindowHandles();
for (String handle : handles) {
    if (!handle.equals(mainWindow)) {
        driver.switchTo().window(handle);
        break;
    }
}

driver.findElement(By.id("23:311;a")).click();

After switching to the new window unable to locate the element

driver.findElement(By.id("23:311;a"))

How can I click the element in the newly opened tab?

HTML: This is salesforce application

<span class="slds-icon_container slds-icon-utility-new-window slds-m-left--x-small focus-icon" data-aura-rendered-by="54:375;a">

23:311;a is not ID attribute, it data-aura-rendered-by attribute and it seems to be random so you can't really relay on that to locate the element. I suggest you use unique class name. lds-icon-utility-new-window seems unique enough

driver.findElement(By.className("lds-icon-utility-new-window"));

Or combination of several class attributes

driver.findElement(By.cssSelector(".slds-icon_container.slds-icon-utility-new-window"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM