在Selenium Java中打开href链接

时间:2016-09-07 14:47:23

标签: java selenium selenium-webdriver

我试图在selenium中单击以下href链接。我尝试过使用xpath和By.linkText()以及By.cssSelectort(),但我无法找到该元素。非常感谢任何帮助

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

By.xpath()但无法找到元素

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

css定位器:

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

以下是 html代码段

<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>

1 个答案:

答案 0 :(得分:0)

尝试使用By.partialLinkText(),如下所示: -

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

已修改: - 如前所述,此元素位于ID为navFramecontentFrame的两个iframe中,您需要逐个切换iframe,然后才能找到元素: -

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();