我正在尝试使用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)。 “Actions”的锚点有一个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>
如何点击selenium的“操作”标签?
这是我收到的异常消息:
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'
答案 0 :(得分:0)
//*[text()='Actions']
与此链接元素不匹配,因为其中包含子元素。相反,我会使用链接的id
:
(new WebDriverWait(webDriver, 10)).until(ExpectedConditions.elementToBeClickable(By.id("btn-contact-actions")));