获取异常org.openqa.selenium.NoSuchElementException:找不到包含文本的链接

时间:2014-03-04 12:31:46

标签: java selenium-webdriver

我正在尝试创建一个将连接到youtube-mp3.org的应用程序。通常在该网站上,我们需要粘贴youtube视频网址,然后点击转换视频按钮。转换完成后会出现带有下载链接的Div。我需要将click命令发送到下载链接。我已经为此编写了如下代码:

public class Example {
public static void main(String[] args) {

    WebDriver driver = new HtmlUnitDriver();

    driver.get("http://www.youtube-mp3.org/");

    // Find the text input element by its id
    WebElement element = driver.findElement(By.id("youtube-url"));

    element.clear();
    element.sendKeys("http://www.youtube.com/watch?v=KMU0tzLwhbE");

    driver.findElement(By.id("submit")).submit();

    driver.findElement(By.linkText("Download")).click();

    // Check the title of the page
    System.out.println("Page title is: " + driver.getTitle());
    System.out.println(driver.getPageSource());

    driver.quit();
}

}

当我运行这个时,我得到了Exception,即:

  Exception in thread "main" org.openqa.selenium.NoSuchElementException: No link found with text:  
    Download
    For documentation on this error,please visit:  http://seleniumhq.org/exceptions/no_such_element.html
   Build info: version: '2.9.0', revision: '14289', time: '2011-10-20 21:53:56'
   System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_45'
   Driver info: driver.version: HtmlUnitDriver
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByLinkText(HtmlUnitDriver.java:650)
at org.openqa.selenium.By$ByLinkText.findElement(By.java:235)
at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1221)
at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:974)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1218)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:394)
at com.general.Example.main(Example.java:38)

我很擅长 Selenium 。请帮我。

感谢。

1 个答案:

答案 0 :(得分:1)

按下提交按钮后,您需要等到链接变为可点击。

import org.openqa.selenium.support.ui.ExpectedConditions;

WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Download")));