用于 enable/disable 用户的单个按钮如何使用 Selenium Webdriver 识别按钮状态
Single button used for enable/disable user how to identify button status using Selenium Webdriver
此处单个按钮用于 enable/disable 用户,当您单击按钮时,如果按钮处于禁用模式,则它变为启用(用户变为活动状态),或者如果按钮处于启用模式,则它变为禁用(用户变为非活动状态)
这里我关心的是如何获取按钮的状态是什么状态,以便我可以根据按钮的状态执行操作。
这是相同的代码片段..
当按钮处于启用模式时。
<a id="13" class="btn btn-xs btn-success inactive" title="Click to Inactivate" href="javascript:void(0);" data-active-inactive="inactive">
<span class="ion-checkmark"></span>
</a>
当按钮处于禁用模式时。
<a id="13" class="btn btn-xs btn-danger active" title="Click to Active" href="javascript:void(0);" data-active-inactive="active">
<span class="ion-android-close"></span>
</a>
这是 front-end 的图像。
你得到元素的属性然后检查它:
Python:
elm = driver.find_element_by_id("13")
class = elm.get_attribute("class")
if "inactive" in class:
print "button is disabled"
else:
print "button is enabled"
Java:
Webelement elm = driver.findElement(By.id("13"));
String c_elm = elm.getAttribute("class")
if c_elm.contains("inactive"){
System.out.println("button is disabled")
}
else{
System.out.println("button is enabled")
}
按钮禁用和启用状态的 class 和标题属性不同。
您可以使用其中任何一个。
对于按钮启用模式:
driver.findElement(By.xpath(@class="btn btn-xs btn-success inactive"))
或
driver.findElement(By.xpath(@title="Click to Inactivate"))
对于按钮禁用状态:
driver.findElement(By.xpath(@class="btn btn-xs btn-danger active"))
或
driver.findElement(By.xpath(@title="Click to Active"))
这是为我编写和工作的代码。
enableDisableVendor = driver.findElement(By.id(enableDisableVendorBtnId));
enableDisableBtnStatus = enableDisableVendor.getAttribute("class");
System.out.println("Button Status :" +enableDisableBtnStatus);
if (enableDisableBtnStatus.equals("btn btn-xs btn-success inactive"))
{
System.out.println("Vendor is Enabled");
}
else if(enableDisableBtnStatus.equals("btn btn-xs btn-danger active"))
{
System.out.println("Vendor is Disabled");
}
else
{
System.out.println("Something went wrong");
}
此处单个按钮用于 enable/disable 用户,当您单击按钮时,如果按钮处于禁用模式,则它变为启用(用户变为活动状态),或者如果按钮处于启用模式,则它变为禁用(用户变为非活动状态) 这里我关心的是如何获取按钮的状态是什么状态,以便我可以根据按钮的状态执行操作。
这是相同的代码片段..
当按钮处于启用模式时。
<a id="13" class="btn btn-xs btn-success inactive" title="Click to Inactivate" href="javascript:void(0);" data-active-inactive="inactive">
<span class="ion-checkmark"></span>
</a>
当按钮处于禁用模式时。
<a id="13" class="btn btn-xs btn-danger active" title="Click to Active" href="javascript:void(0);" data-active-inactive="active">
<span class="ion-android-close"></span>
</a>
这是 front-end 的图像。
你得到元素的属性然后检查它:
Python:
elm = driver.find_element_by_id("13")
class = elm.get_attribute("class")
if "inactive" in class:
print "button is disabled"
else:
print "button is enabled"
Java:
Webelement elm = driver.findElement(By.id("13"));
String c_elm = elm.getAttribute("class")
if c_elm.contains("inactive"){
System.out.println("button is disabled")
}
else{
System.out.println("button is enabled")
}
按钮禁用和启用状态的 class 和标题属性不同。
您可以使用其中任何一个。
对于按钮启用模式:
driver.findElement(By.xpath(@class="btn btn-xs btn-success inactive"))
或
driver.findElement(By.xpath(@title="Click to Inactivate"))
对于按钮禁用状态:
driver.findElement(By.xpath(@class="btn btn-xs btn-danger active"))
或
driver.findElement(By.xpath(@title="Click to Active"))
这是为我编写和工作的代码。
enableDisableVendor = driver.findElement(By.id(enableDisableVendorBtnId));
enableDisableBtnStatus = enableDisableVendor.getAttribute("class");
System.out.println("Button Status :" +enableDisableBtnStatus);
if (enableDisableBtnStatus.equals("btn btn-xs btn-success inactive"))
{
System.out.println("Vendor is Enabled");
}
else if(enableDisableBtnStatus.equals("btn btn-xs btn-danger active"))
{
System.out.println("Vendor is Disabled");
}
else
{
System.out.println("Something went wrong");
}