【问题标题】:How to identify a webelement in Selenium if it cannot be identified even with xpath如果即使使用 xpath 也无法识别,如何在 Selenium 中识别 webelement
【发布时间】:2017-05-20 12:09:23
【问题描述】:

我正在尝试在 Selenium 中识别一个链接(链接文本是“Pearson eText”)。我尝试过使用 By.linkText、By.xpath 甚至 By.tagName,但这些似乎都不起作用。我想知道是否有人可以让我知道是否有更多方法可以识别元素。以下是网页中的 html 代码。提前致谢!


<div class="container_12" style="margin-bottom:10px;">
            <div class="grid_5" id="studlinks"><h3><a href="javascript:launch('71053')">Welcome to MasteringAandP</a></h3><p>Access all your Mastering course resources and assignments
</p><h3><a href="javascript:launch('164')">Mastering Assignments</a></h3><p>Access Mastering homework assignments.</p><h3><a href="javascript:launch('165')">Mastering Scores (for students)</a></h3><p>Access your results for all Mastering work you have completed. </p><h3><a href="javascript:launch('170')">User Settings</a></h3><p>Manage your Pearson account information.</p><h3><a href="javascript:launch('171')">Pearson eText</a></h3><p>Access your Pearson eText.</p><h3><a href="javascript:launch('172')">Study Area</a></h3><p>Access the self Study Area to watch videos and animations, take practice quizzes and more.  Your work in the Study Area does not report to your instructor's grade book. To complete instructor-assigned activities, go to Assignments.</p><h3><a href="javascript:launch('105')">MasteringAandP Course Home</a></h3><p>Access your MasteringAandP course for additional content and assignments.</p></div>
            <div class="grid_5" id="instlinks"></div>
        </div>

【问题讨论】:

  • 当您通过链接文本进行搜索时,您是使用"Access your Pearson eText" 还是仅使用"Pearson eText"

标签: selenium xpath


【解决方案1】:

有几种方法。 CSS 选择器方式之一可能是:

WebElement personETextLink = driver.findElement(By.cssSelector("a[href=\"javascript:launch('171')\"]"));

而且,如果你选择 xpath,它不会让你失望。

WebElement personETextLink = driver.findElement(By.xpath("//*[@href=\"javascript:launch('171')\"]"));

【讨论】:

    【解决方案2】:

    通过 xpath 查找 Link 元素:

    //a[text()='Pearson eText']
    

    如果你想得到href,你可以很容易地做到:

    //a[text()='Pearson eText']/@href
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-05
      • 2016-09-21
      相关资源
      最近更新 更多