使用 cssSelector 或 xPath Selenium Web 驱动程序在过滤器上应用按钮?
Apply Button on Filter with cssSelector or xPath Selenium Web Driver?
我在源代码中有这部分:
<div class="CFApplyButtonContainer" style="height: 21px;">
<button class="tab-button tab-widget disabled" style="max-width: 67px;" disabled="" type="button">
<span class="icon"></span>
<span class="label">Cancel</span>
</button>
<button class="tab-button tab-widget disabled" style="max-width: 67px;" disabled="" type="button">
<span class="icon"></span>
<span class="label">Apply</span>
</button>
</div>
而我的 Java 部分是这样的:
// detect type of the filter
if (type.equals("")) {
elementList = driver.findElements(By.xpath("//div[@id='" + id + "_menu']//a[@class='FIText']"));
if (elementList.size() > 0) {
if (driver.findElements(By.xpath("//div[@id='" + id + "_menu']//div[@class='CFApplyButtonContainer']//span[@class='label'][text()='Apply']/..")).size() > 0) {
type = "multi_checkbox_with_apply";
}
else {
type = "multi_checkbox_without_apply";
}
}
}
//if apply button enabled
element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='"+id+"_menu']//div[@class='CFApplyButtonContainer']//span[@class='label'][text()='Apply']/..")));
if (element.getAttribute("disabled") == null) {
element.click();
//log("clicked on apply button");
waitForLoadingSpinner();
}
else {
//log("apply button disabled - no need to click on it");
}
// close drop down menu
element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='tab-glass clear-glass tab-widget']")));
element.click();
//log("dropdown menu closed");
}
这是找到包含 "Apply" 的跨度的正确方法吗?我的申请突然停止工作,不知道还能尝试什么?
搜索标签内文本的唯一方法(除了查找仅适用于 A
标签的 link 文本)是使用 XPath。下面应该得到你想要的。
//div[@class='CFApplyButtonContainer']/button/span[text()='Apply']
像您在问题中所做的那样使用它
if (driver.findElements(By.xpath("//div[@class='CFApplyButtonContainer']/button/span[text()='Apply']")).size() > 0)
{
// found the element, do stuff
}
我在源代码中有这部分:
<div class="CFApplyButtonContainer" style="height: 21px;">
<button class="tab-button tab-widget disabled" style="max-width: 67px;" disabled="" type="button">
<span class="icon"></span>
<span class="label">Cancel</span>
</button>
<button class="tab-button tab-widget disabled" style="max-width: 67px;" disabled="" type="button">
<span class="icon"></span>
<span class="label">Apply</span>
</button>
</div>
而我的 Java 部分是这样的:
// detect type of the filter
if (type.equals("")) {
elementList = driver.findElements(By.xpath("//div[@id='" + id + "_menu']//a[@class='FIText']"));
if (elementList.size() > 0) {
if (driver.findElements(By.xpath("//div[@id='" + id + "_menu']//div[@class='CFApplyButtonContainer']//span[@class='label'][text()='Apply']/..")).size() > 0) {
type = "multi_checkbox_with_apply";
}
else {
type = "multi_checkbox_without_apply";
}
}
}
//if apply button enabled
element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='"+id+"_menu']//div[@class='CFApplyButtonContainer']//span[@class='label'][text()='Apply']/..")));
if (element.getAttribute("disabled") == null) {
element.click();
//log("clicked on apply button");
waitForLoadingSpinner();
}
else {
//log("apply button disabled - no need to click on it");
}
// close drop down menu
element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='tab-glass clear-glass tab-widget']")));
element.click();
//log("dropdown menu closed");
}
这是找到包含 "Apply" 的跨度的正确方法吗?我的申请突然停止工作,不知道还能尝试什么?
搜索标签内文本的唯一方法(除了查找仅适用于 A
标签的 link 文本)是使用 XPath。下面应该得到你想要的。
//div[@class='CFApplyButtonContainer']/button/span[text()='Apply']
像您在问题中所做的那样使用它
if (driver.findElements(By.xpath("//div[@class='CFApplyButtonContainer']/button/span[text()='Apply']")).size() > 0)
{
// found the element, do stuff
}