My code is below, I am having problems with 'Production Plan', I need to be able to click the Production Plan link but it doesn't work.
List<WebElement> ddOpts = driver.findElements(By.xpath("html/body/div[4]"));
ArrayList<String> links = new ArrayList<String>();
for(WebElement we : ddOpts) {
//System.out.println(we.getText());
links.add(we.getText());
System.out.println(links);
if(we.getText().contains("Production Plan")) {
we.sendKeys("Production Plan");
we.click();
}
2 Answers 2
Your WebElements in ddOpts list aren't an anchor tags but divs. I don't know how does the page look, but it seems you might thought another xpath. Something like:
List<WebElement> ddOpts = driver.findElements(By.xpath("html/body/div/a[4]"));
or
List<WebElement> ddOpts = driver.findElements(By.xpath("html/body/div[4]/a"));
Or maybe if it is a select option, use a Select object
Select mySelect = new Select(driver.findElements(By.xpath("html/body/div[4]")));
mySelect.selectByVisibleText("Production Plan");
see answer of this question: How to select an option from a drop-down using Selenium WebDriver with Java?
Comments
I didn't understand why your trying to sendKeys()
.
But If you are trying to click on a link, following will work :
WebElement link = driver.findElement(By.PartialLinkText("Production Plan"));
link.click();
or
You can also try with Explicit wait :
new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOfElementLocated(By.PartialLinkText("Production Plan"))).click();