【问题标题】:How to write code for linkText for the html code如何为 html 代码的 linkText 编写代码
【发布时间】:2020-01-11 01:32:19
【问题描述】:

我是 selenium 新手,正在尝试为 html 代码编写链接文本代码

<a href="repopulateUser.jsp?authAuthSessionId=7DD74346650647B7BA9ED08C1ABAE66D&amp;roleId=bc95f1f2-9ccd-11e8-9a37-0050568817ef&amp;ROLE=Programmer&amp;ROLECODE=PROGRAMMER&amp;roleGrpId=f4c11ca7-9c1b-11e8-9a37-0050568817ef&amp;moduleId=cb475927-7eb1-11e8-97d0-0050568817ef&amp;studyId=null" class="labels">Programmer</a>

代码试用:

driver.findElement(By.linkText("Programmer")).click();   

上面的代码不起作用,有什么问题吗?

【问题讨论】:

  • 评论中提到了html代码。
  • “不工作”是什么意思?它会抛出错误吗?如果是这样,请编辑您的问题并发布错误消息。
  • 它没有输出。
  • 期望什么样的输出?

标签: java selenium xpath css-selectors linktext


【解决方案1】:
如果带有文本 Programmer 的元素在 HTML 中是唯一标识的,则

linkText 应该可以工作。仍然因为该元素是启用了JavaScript 的元素,所以要在需要使用elementToBeClickable() 的元素上使用click(),您可以使用以下Locator Strategy

  • linkText:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Programmer"))).click();
    

另类

您也可以使用以下任一Locator Strategies

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.labels[href^='repopulateUser']"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='labels' and starts-with(@href, 'repopulateUser')][text()='Programmer']"))).click();
    

【讨论】:

    猜你喜欢
    • 2023-04-05
    • 2012-07-11
    • 2019-11-30
    • 2019-07-23
    • 1970-01-01
    • 2016-05-05
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多