Buras
Buras

Reputation: 3099

Selenium Java. Navigating WebElement by linkText issue

I am running a Selenium test. I have link that I need to click. I have done many links, but this one does not allow me to click it...However, it works perfectly fine when I do it manually! Here is its html of the link element:

 <a href="form_general_power.asp" onclick="resetSearch();" target="mainFrame">Advanced Search</a>

I tried each of the following

WebElement element = driver.findElement(By.xpath("//a [@href='form_general_power.asp']"));
 WebElement element = driver.findElement(By.xpath("//a [@onclick='resetSearch();']"));
 WebElement element = driver.findElement(By.xpath("//a [@target='mainFrame']"));
 WebElement element = driver.findElement(By.linkText("Advanced Search"));

element.click();

Is there any other method of dealing with a link.

Upvotes: 2

Views: 3034

Answers (2)

Tuzik Tuzikov
Tuzik Tuzikov

Reputation: 21

You can also try using the css selector.

WebElement element = driver.findElement(By.cssSelector("/..."));

The problem with the linkText is that is is sometimes is not responsive element.click();

Upvotes: 2

so cal cheesehead
so cal cheesehead

Reputation: 2573

Try

WebElement element = driver.findElement(By.xpath("//a[text()=\"Advanced Search\"]"));
element.click();

Note that there are no spaces in the xpath like you have in your examples.

Upvotes: 1

Related Questions