使用 selenium 处理动态 webtable java
Handling dynamic webtable using selenium java
我正在尝试单击一个特定的公司并从该网站的控制台中打印公司名称 http://demo.guru99.com/test/web-table-element.php# 我没有得到这样的元素异常
这是我的代码:
driver.findElement(By.xpath("//a[contains(text(),'Marico Ltd.')]/parent::td")).click();
您应该获取元素列表并检查其大小是否存在:
List<WebElement> list = driver.findElements(By.xpath("//a[contains(text(),'Marico Ltd.')]"));
Assert.assertTrue("Text not found!", list.size() > 0);
此外,您不应在 findElement
中使用 /parent::td
。接下来可以点击断言后的元素
List.get(0).click()
您可以使用循环搜索元素,刷新页面直至显示。
注意:如果元素不显示会进入无限循环,所以在某个时候打破它。
方法#1:循环检查10次是否显示所需元素。
driver.get("http://demo.guru99.com/test/web-table-element.php#");
int i = 0;
while (i < 10) {
if (isElementDisplayed()) {
driver.findElement(By.xpath("//a[contains(text(),'Marico Ltd.')]/parent::td")).click();
System.out.println("Navigated to Guru99 Bank at " + i + " iteration.");
break;
} else {
driver.navigate().refresh();
i++;
}
}
方法#2:检查 returns boolean
值为 true
if element exists else returns false
public static boolean isElementDisplayed() {
try {
driver.findElement(By.xpath("//a[contains(text(),'Marico Ltd.')]/parent::td"));
return true;
} catch (org.openqa.selenium.NoSuchElementException e) {
return false;
}
}
输出:
Navigated to Guru99 Bank at 4 iteration.
您的定位器不一定是错误的,但在这种情况下它不起作用,但我们可以修复它。问题在于,当 Selenium 尝试单击某个元素时,它会找到该元素的 x-y 维度,然后单击准确的中心。在这种情况下,TD 的确切中心错过了 A 标记(超链接)。
解决此问题的最简单方法是使用定位器单击 A 标签,
//a[contains(text(),'Marico Ltd.')]
使用 WebDriverWait
始终是最佳做法,以确保在对元素执行操作之前它已准备就绪。
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(text(),'Marico Ltd.')]"))).click();
我正在尝试单击一个特定的公司并从该网站的控制台中打印公司名称 http://demo.guru99.com/test/web-table-element.php# 我没有得到这样的元素异常 这是我的代码:
driver.findElement(By.xpath("//a[contains(text(),'Marico Ltd.')]/parent::td")).click();
您应该获取元素列表并检查其大小是否存在:
List<WebElement> list = driver.findElements(By.xpath("//a[contains(text(),'Marico Ltd.')]"));
Assert.assertTrue("Text not found!", list.size() > 0);
此外,您不应在 findElement
中使用 /parent::td
。接下来可以点击断言后的元素
List.get(0).click()
您可以使用循环搜索元素,刷新页面直至显示。
注意:如果元素不显示会进入无限循环,所以在某个时候打破它。
方法#1:循环检查10次是否显示所需元素。
driver.get("http://demo.guru99.com/test/web-table-element.php#");
int i = 0;
while (i < 10) {
if (isElementDisplayed()) {
driver.findElement(By.xpath("//a[contains(text(),'Marico Ltd.')]/parent::td")).click();
System.out.println("Navigated to Guru99 Bank at " + i + " iteration.");
break;
} else {
driver.navigate().refresh();
i++;
}
}
方法#2:检查 returns boolean
值为 true
if element exists else returns false
public static boolean isElementDisplayed() {
try {
driver.findElement(By.xpath("//a[contains(text(),'Marico Ltd.')]/parent::td"));
return true;
} catch (org.openqa.selenium.NoSuchElementException e) {
return false;
}
}
输出:
Navigated to Guru99 Bank at 4 iteration.
您的定位器不一定是错误的,但在这种情况下它不起作用,但我们可以修复它。问题在于,当 Selenium 尝试单击某个元素时,它会找到该元素的 x-y 维度,然后单击准确的中心。在这种情况下,TD 的确切中心错过了 A 标记(超链接)。
解决此问题的最简单方法是使用定位器单击 A 标签,
//a[contains(text(),'Marico Ltd.')]
使用 WebDriverWait
始终是最佳做法,以确保在对元素执行操作之前它已准备就绪。
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(text(),'Marico Ltd.')]"))).click();