kurri
kurri

Reputation: 57

I am trying to click the Link to open dialog box in selenium web driver

Here is my Html Code:

<a id="expTo" class="formblue_link padRight10 exportLinkActive" 
      href="javascript:;" style="display: block; margin-left: -50px; margin- bottom: -20px;"> Export To </a>

I tried below Code:

 new WebDriverWait(driver, 50).until(ExpectedConditions.elementToBeClickable(By.linkText("expTo"))).click();

But I am unable to click on the link.

Upvotes: 0

Views: 377

Answers (1)

Saurabh Gaur
Saurabh Gaur

Reputation: 23825

If you are locating element using id attribute value of the link element, It should be By.id() instead as :-

new WebDriverWait(driver, 50).until(ExpectedConditions.elementToBeClickable(By.id("expTo"))).click();

Or if you want to locate element using By.linkText(), you should try to pass exact innerText of the link element as :-

new WebDriverWait(driver, 50).until(ExpectedConditions.elementToBeClickable(By.linkText(" Export To "))).click();

Note:- As I'm seeing in your provided HTML for link element's innerText has extra spaces, in this case if you are using By.linkText(), you have to pass element's innerText with extra spaces as well. If there extra spaces is present, you can also try using By.partialLinkText() to pass only visible text without using spaces as :-

new WebDriverWait(driver, 50).until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Export To"))).click();

Edited

It is inside IFrame Saurabh. How can I try to click?

If this element is inside an iframe, you need to switch that iframe before finding element as below :-

WebDriverWait wait = new WebDriverWait(driver, 50);

//Now switch to iframe 
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("iframe id or name"));

//Now find desire element click 
wait.until(ExpectedConditions.elementToBeClickable(By.id("expTo"))).click();

//After doing all stuff inside iframe switch back to default content for further steps
driver.switchTo().defaultContent(); 

Upvotes: 2

Related Questions