简体   繁体   中英

Clicking a javascript generated link in selenium

I am trying to export the contacts of a yahoo account in an app using selenium. The code I'm using is:

private static void getYahooContacts(Map.Entry entry) { // username in key, passwd in entry WebDriver webDriver = new FirefoxDriver();

    webDriver.get("https://login.yahoo.com/config/login_verify2?.intl=us&.src=ym");
    WebElement userName = webDriver.findElement(By.id("login-username"));
    userName.sendKeys(entry.getKey());
    WebElement userPassword = webDriver.findElement(By.id("login-passwd"));
    userPassword.sendKeys(entry.getValue());
    WebElement signInButton = webDriver.findElement(By.id("login-signin"));
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        System.out.println("Thread couldn't sleep.");
    }
    signInButton.click();
    signInButton.click();

    // Since yahoo is rendered through javascript we wait for it at most 10 seconds to finish
    (new WebDriverWait(webDriver, 10)).until(new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver d) {
            return true;
        }
    });
    // click on the contacts tab (upper left)
    List<WebElement> menuItems = (List<WebElement>) webDriver.findElements(By.className("nav-lnk"));
    menuItems.get(1).click();
    // so far so good
    // this always fails
    // (new WebDriverWait(webDriver, 10)).until(ExpectedConditions.elementToBeClickable(By.id("btn-contact-actions")));
    (new WebDriverWait(webDriver, 10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[text()='Actions']")));

    WebElement actionsButton = webDriver.findElement(By.className("btn menu btn-actions"));
    actionsButton.click();

    (new WebDriverWait(webDriver, 2)).until(new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver d) {
            return true;
        }
    });

    List<WebElement> exportButtons = (List<WebElement>) webDriver.findElement(By.xpath("//li[@class='dlg']"));
    exportButtons.get(1).click();

    //Close the browser
    webDriver.quit();

}

The code runs until it has to click the "Actions" link in the contacts menu when an exception is raised (the xpath to the link is not found). The anchor for "Actions" has an id:

<a id="btn-contact-actions" class="btn menu btn-actions" data-action="menu" title="More actions for selected contacts" href="#">
<i class="icon-more"></i>
<span class="icon-text">Actions</span>
<i class="icon-chevron-down"></i>
</a>

How can I click the "Actions" tab from selenium?

This is the exception message I get:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"btn-contact-actions"}
Command duration or timeout: 115 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.49.0', revision: '365eeb44deba2067b1761c8862ef21d55250e063', time: '2016-01-13 11:57:39'

The //*[text()='Actions'] would not match this link element, since there are child elements inside it. Instead, I would use the id of the link:

(new WebDriverWait(webDriver, 10)).until(ExpectedConditions.elementToBeClickable(By.id("btn-contact-actions")));

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