繁体   English   中英

单击硒中的javascript生成的链接

[英]Clicking a javascript generated link in selenium

我正在尝试使用硒在应用程序中导出Yahoo帐户的联系人。 我使用的代码是:

private static void getYahooContacts(Map.Entry entry){//密钥中的用户名,条目中的passwd 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();

}

该代码将一直运行,直到引发异常(找不到链接的xpath)时,必须单击联系人菜单中的“操作”链接。 “动作”的锚点具有一个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>

如何单击硒中的“操作”标签?

这是我收到的异常消息:

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'

//*[text()='Actions']将不匹配此链接元素,因为其中包含子元素。 相反,我将使用链接的id

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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