Selenium webdriver 如何使用selenium Webdriver单击链接

Selenium webdriver 如何使用selenium Webdriver单击链接,selenium-webdriver,hyperlink,Selenium Webdriver,Hyperlink,如何单击具有相同名称的selenium Webdriver的链接 driver.findElement(By.linkText("View All")).click(); 还有一些其他链接也有相同的名称,如查看所有您应该尝试使用xpath查找具有此链接其他属性组合(如类名和文本)的唯一链接,如下所示:- WebDriverWait wait = new WebDriverWait(driver, 10); WebElement link = wait.until(ExpectedConditi

如何单击具有相同名称的selenium Webdriver的链接

driver.findElement(By.linkText("View All")).click();

还有一些其他链接也有相同的名称,如
查看所有

您应该尝试使用
xpath
查找具有此链接其他属性组合(如类名和文本)的唯一链接,如下所示:-

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement link = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//a[contains(@class, 'absence-viewall') and contains(text(), 'View All')]")));
link.click();
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement link = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("a.absence-viewall")));
link.click();
或者,如果此链接具有唯一的类名,则使用.cssSelector()的
的最佳方法如下所示:-

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement link = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//a[contains(@class, 'absence-viewall') and contains(text(), 'View All')]")));
link.click();
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement link = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("a.absence-viewall")));
link.click();

您应该尝试使用
xpath
来定位具有此链接的其他属性组合(如类名和文本)的唯一链接,如下所示:-

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement link = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//a[contains(@class, 'absence-viewall') and contains(text(), 'View All')]")));
link.click();
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement link = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("a.absence-viewall")));
link.click();
或者,如果此链接具有唯一的类名,则使用.cssSelector()的
的最佳方法如下所示:-

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement link = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//a[contains(@class, 'absence-viewall') and contains(text(), 'View All')]")));
link.click();
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement link = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("a.absence-viewall")));
link.click();

你能发布示例HTML代码吗?你提到你有多个“查看所有”链接ryt?它们是否存在于具有不同用户名的表中?这两个小部件是否具有不同的类名或任何不同的属性?请发布这两个小部件的HTML代码。这将帮助我们为您提供一些可靠的解决方案。请发布示例HTML代码好吗?您提到您有多个“查看所有”链接ryt?它们是否存在于具有不同用户名的表中?这两个小部件是否具有不同的类名或任何不同的属性?请发布这两个小部件的HTML代码。这将帮助我们为您提供一些可靠的解决方案。