簡體   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