我无法找到带有锚标记的定位器。使用xpath不会给出结果。
以下是HTML代码:
<a id="sme1" class="item" href="/website-url" onclick="swapClasses('sme1')"
target="main" style="background-color:
transparent;">Mailboxes</a>
Selenium代码:
private static final String baseURL = "http://10.112.75.248/";
private static final String adminURL = baseURL + "manager/";
private static String username = "admin";
private static String password = "default";
WebDriver driver ;
SearchElement searchEl;
@BeforeTest
public void setBaseURL(){
driver = new FirefoxDriver();
driver.get(adminURL);
searchEl = new SearchElement();
}
@Test(priority=0)
public void verifyLoginSuccessFull() {
searchEl.getElementsByXpath(driver, "//input[@name='username']").sendKeys(username);
searchEl.getElementsByXpath(driver, "//input[@name='password']").sendKeys(password);
searchEl.getElementsByXpath(driver, "//input[@name='submit']").submit();
String expected = "Selenium Home Page";
Assert.assertEquals(driver.getTitle(), expected);
}
@Test(priority=1)
public void addMailbox(){
//WebDriverWait wait = new WebDriverWait(driver, 1000);
//wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[text()='Mailboxes']")));
//driver.findElement(By.id("smel")).click();
searchEl.getElementsByLinkText(driver, "//a[text()='Mailboxes']").click(); //This line of code not fetching the locator.
}
用作控制器的SearchElement类:
public class SearchElement {
public WebElement getElementsByXpath(WebDriver driver,String locator){
WebElement el = driver.findElement(By.xpath(locator));
return el;
}
public WebElement getElementsByLinkText(WebDriver driver,String locator){
WebElement el = driver.findElement(By.linkText(locator));
return el;
}
}
实际错误:
无法找到元素:{&#34;方法&#34;:&#34;链接 文本&#34;&#34;选择器&#34;:&#34; //一个[文本()=&#39;邮箱&#39;]&#34;}
注意:当用户登录时以及向用户显示登录页面时,URL会有所不同。我不知道这可能是个问题。
请提前帮助。
答案 0 :(得分:1)
By.linkText()
方法需要链接文本作为参数,但在该代码中您传递 XPath字符串。尝试传递链接文本:
searchEl.getElementsByLinkText(driver, "Mailboxes").click();
答案 1 :(得分:0)
试试这个。
//a[.='Mailboxes']
这是一个基于文本的搜索和&#34;。&#34;在xpath内部导航到父