ccook14 ccook14 - 2 months ago 40
Java Question

Opening a href link in Selenium Java

I am trying to click on the following a href link in selenium. I have tried using xpath and

By.linkText()
and
By.cssSelectort()
but I have not be able to locate the element. Any help is much appreciated

driver.findElement(By.linkText("CCC_PH3_Sandbox_Keybridge: CCC PH3 Sandbox Keybridge")).click();


and
By.xpath()
but its unable to locate the element

WebElement course = driver.findElementByXPath("html/body/table/tbody/tr/td/div/div[2]/table/tbody/tr/td[3]/a/span[1]");
course.click();


css locator :

driver.findElement(By.cssSelector("a[href*='/webapps/portal']")).click();


Here is the html snippet:

<img width="12" height="12" src="/images/ci/icons/bookopen_li.gif" alt="">
<a target="_top" href=" /webapps/portal/frameset.jsp?tab_tab_group_id=_2_1&url=%2Fwebapps%2Fblackboard%2Fexecute%2Flauncher%3Ftype%3DCourse%26id%3D_2135_1%26url%3D">CCC_PH3_Sandbox_Keybridge: CCC PH3 Sandbox Keybridge</a>

Answer

Try using By.partialLinkText() as below :-

new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.partialLinkText("CCC_PH3_Sandbox_Keybridge"))).click();

Edited:- As you have mentioned this element is inside two iframes with id navFrame and contentFrame, you need to switch that iframes one by one before finding element as :-

driver.switchTo().frame("navFrame");
driver.switchTo().frame("contentFrame");

//Now find desire element 
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.partialLinkText("CCC_PH3_Sandbox_Keybridge"))).click();

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