我正在尝试在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>
答案 0 :(得分:0)
有几种方法。 CSS选择器的一种方式可能是:
WebElement personETextLink = driver.findElement(By.cssSelector("a[href=\"javascript:launch('171')\"]"));
而且,如果你选择xpath,它不会让你绝望。
WebElement personETextLink = driver.findElement(By.xpath("//*[@href=\"javascript:launch('171')\"]"));
答案 1 :(得分:0)
通过xpath查找Link元素:
//a[text()='Pearson eText']
如果你想获得href,你可以轻松地做到:
//a[text()='Pearson eText']/@href