Reputation: 1178
I'm trying to make an application that will connect to youtube-mp3.org. Normally on that site, we need to paste the youtube video url and click on Convert Video button. Then a Div with download link appears after conversion is done. All I need to send click command to the download link. I've written code for this which is as follows :
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();
}
}
When I run this I get Exception which is:
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)
I'm pretty new to Selenium. Please help me.
Thanks.
Upvotes: 0
Views: 849
Reputation: 677
You need to wait until the link becomes clickable, after pressing the submit button.
import org.openqa.selenium.support.ui.ExpectedConditions;
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Download")));
Upvotes: 1