selenium comboTree 动态加载树里的项怎么定位
本帖最后由 bjj1985 于 2016-4-1 08:09 编辑如图1里的,我想选北京市-市辖区下面的东城区,应该怎么实现
我自己解决了,贴一下
public String selectDepartmentForAddEmployee(WebDriver driver) {
// 点击了一级部门
int num1 = getElements(driver, By.xpath("//li[@level='1']"));
// logD("一级部门有:" + num1 + "个");
int rDemt = getIndex(num1) / 2 + 1;
driver.findElement(By.cssSelector("#dName")).click();
delay(500);
// 点击第一层
String xpath = String.format("//*[@id='userModal']/form/div/div/div/div/div/div/div/ul/ul/li[%d]/i",
rDemt);
String xpath_1 = String.format("//*[@id='userModal']/form/div/div/div/div/div/div/div/ul/ul/li[%d]",
rDemt);
// logD("一级菜单的地址为: 【" + rDemt + "】【" + xpath + "】");
clickByxpath(driver, xpath, 500);
// check 第二层是否存在,如果不存在则返回
int num2 = getElements(driver, By.xpath("//li[@level='2']"));
// logD("二级部门有:" + num2 + "个");
if (num2 > 0) {
int rDemt2 = getIndex(num2) + 1;
String xpath2 = String
.format("//*[@id='userModal']/form/div/div/div/div/div/div/div/ul/ul/ul/li[%d]/i", rDemt2);
String xpath2_1 = String
.format("//*[@id='userModal']/form/div/div/div/div/div/div/div/ul/ul/ul/li[%d]", rDemt2);
// logD("二级菜单的地址为: 【" + rDemt2 + "】【" + xpath2 + "】");
clickByxpath(driver, xpath2, 500);
// check 第三层是否存在,如果不存在则返回
int num3 = getElements(driver, By.xpath("//li[@level='3']"));
// logD("三级部门有:" + num3 + "个");
if (num3 > 0) {
int rDemt3 = getIndex(num3) + 1;
String xpath3 = String.format(
"//*[@id='userModal']/form/div/div/div/div/div/div/div/ul/ul/ul/ul/li[%d]/i", rDemt3);
String xpath3_1 = String.format(
"//*[@id='userModal']/form/div/div/div/div/div/div/div/ul/ul/ul/ul/li[%d]", rDemt3);
// logD("三级菜单的地址为: 【" + rDemt3 + "】【" + xpath3 + "】");
clickByxpath(driver, xpath3, 500);
// check 第四层是否存在,如果不存在则返回
int num4 = getElements(driver, By.xpath("//li[@level='4']"));
// logD("四级部门有:" + num4 + "个");
if (num4 > 0) {
int rDemt4 = getIndex(num4) + 1;
String xpath4 = String.format(
"//*[@id='userModal']/form/div/div/div/div/div/div/div/ul/ul/ul/ul/ul/li[%d]/i",
rDemt4);
String xpath4_1 = String.format(
"//*[@id='userModal']/form/div/div/div/div/div/div/div/ul/ul/ul/ul/ul/li[%d]",
rDemt4);
// logD("四级菜单的地址为: 【" + rDemt4 + "】【" + xpath4 + "】");
clickByxpath(driver, xpath4, 500);
// check 第五层是否存在,如果不存在则返回
int num5 = getElements(driver, By.xpath("//li[@level='5']"));
// logD("五级部门有:" + num5 + "个");
if (num5 > 0) {
int rDemt5 = getIndex(num5) + 1;
String xpath5 = String.format(
"//*[@id='userModal']/form/div/div/div/div/div/div/div/ul/ul/ul/ul/ul/ul/li[%d]/i",
rDemt5);
// logD("五级菜单的地址为: 【" + rDemt5 + "】【" + xpath5 + "】");
clickByxpath(driver, xpath5, 500);
} else {
logD("【四级部门: " + getText(driver, By.xpath(xpath4_1), "data-text") + "】");
return getText(driver, By.xpath(xpath4_1), "data-text");
}
} else {
logD("【三级部门: " + getText(driver, By.xpath(xpath3_1), "data-text") + "】");
return getText(driver, By.xpath(xpath3_1), "data-text");
}
} else {
logD("【二级部门: " + getText(driver, By.xpath(xpath2_1), "data-text") + "】");
return getText(driver, By.xpath(xpath2_1), "data-text");
}
} else {
logD("【一级部门: " + getText(driver, By.xpath(xpath_1), "data-text") + "】");
return getText(driver, By.xpath(xpath_1), "data-text");
}
return null;
}
谷歌或者火狐浏览器,直接右键,点击审查,找到东城区,右键-copy xpath或者copy selector.这是自动获取的;当然你也可以手动去获取 wuxi88 发表于 2016-3-30 10:11
谷歌或者火狐浏览器,直接右键,点击审查,找到东城区,右键-copy xpath或者copy selector.这是自动获取的 ...
这我知道的,这个是动态获取的,只有点了北京后,才会显示市辖区和县,这是动态获取的。我现在能想到的就是点一下后去获取一下,看有没有东西,但是这个方法不好写,这个xpath的地址是一直在变的。 public String selectDepartmentForAddEmployee(WebDriver driver) {
// 点击了一级部门
int num1 = getElements(driver, By.xpath("//li[@level='1']"));
// logD("一级部门有:" + num1 + "个");
int rDemt = getIndex(num1) / 2 + 1;
driver.findElement(By.cssSelector("#dName")).click();
delay(500);
// 点击第一层
String xpath = String.format("//*[@id='userModal']/form/div/div/div/div/div/div/div/ul/ul/li[%d]/i",
rDemt);
String xpath_1 = String.format("//*[@id='userModal']/form/div/div/div/div/div/div/div/ul/ul/li[%d]",
rDemt);
// logD("一级菜单的地址为: 【" + rDemt + "】【" + xpath + "】");
clickByxpath(driver, xpath, 500);
// check 第二层是否存在,如果不存在则返回
int num2 = getElements(driver, By.xpath("//li[@level='2']"));
// logD("二级部门有:" + num2 + "个");
if (num2 > 0) {
int rDemt2 = getIndex(num2) + 1;
String xpath2 = String
.format("//*[@id='userModal']/form/div/div/div/div/div/div/div/ul/ul/ul/li[%d]/i", rDemt2);
String xpath2_1 = String
.format("//*[@id='userModal']/form/div/div/div/div/div/div/div/ul/ul/ul/li[%d]", rDemt2);
// logD("二级菜单的地址为: 【" + rDemt2 + "】【" + xpath2 + "】");
clickByxpath(driver, xpath2, 500);
// check 第三层是否存在,如果不存在则返回
int num3 = getElements(driver, By.xpath("//li[@level='3']"));
// logD("三级部门有:" + num3 + "个");
if (num3 > 0) {
int rDemt3 = getIndex(num3) + 1;
String xpath3 = String.format(
"//*[@id='userModal']/form/div/div/div/div/div/div/div/ul/ul/ul/ul/li[%d]/i", rDemt3);
String xpath3_1 = String.format(
"//*[@id='userModal']/form/div/div/div/div/div/div/div/ul/ul/ul/ul/li[%d]", rDemt3);
// logD("三级菜单的地址为: 【" + rDemt3 + "】【" + xpath3 + "】");
clickByxpath(driver, xpath3, 500);
// check 第四层是否存在,如果不存在则返回
int num4 = getElements(driver, By.xpath("//li[@level='4']"));
// logD("四级部门有:" + num4 + "个");
if (num4 > 0) {
int rDemt4 = getIndex(num4) + 1;
String xpath4 = String.format(
"//*[@id='userModal']/form/div/div/div/div/div/div/div/ul/ul/ul/ul/ul/li[%d]/i",
rDemt4);
String xpath4_1 = String.format(
"//*[@id='userModal']/form/div/div/div/div/div/div/div/ul/ul/ul/ul/ul/li[%d]",
rDemt4);
// logD("四级菜单的地址为: 【" + rDemt4 + "】【" + xpath4 + "】");
clickByxpath(driver, xpath4, 500);
// check 第五层是否存在,如果不存在则返回
int num5 = getElements(driver, By.xpath("//li[@level='5']"));
// logD("五级部门有:" + num5 + "个");
if (num5 > 0) {
int rDemt5 = getIndex(num5) + 1;
String xpath5 = String.format(
"//*[@id='userModal']/form/div/div/div/div/div/div/div/ul/ul/ul/ul/ul/ul/li[%d]/i",
rDemt5);
// logD("五级菜单的地址为: 【" + rDemt5 + "】【" + xpath5 + "】");
clickByxpath(driver, xpath5, 500);
} else {
logD("【四级部门: " + getText(driver, By.xpath(xpath4_1), "data-text") + "】");
return getText(driver, By.xpath(xpath4_1), "data-text");
}
} else {
logD("【三级部门: " + getText(driver, By.xpath(xpath3_1), "data-text") + "】");
return getText(driver, By.xpath(xpath3_1), "data-text");
}
} else {
logD("【二级部门: " + getText(driver, By.xpath(xpath2_1), "data-text") + "】");
return getText(driver, By.xpath(xpath2_1), "data-text");
}
} else {
logD("【一级部门: " + getText(driver, By.xpath(xpath_1), "data-text") + "】");
return getText(driver, By.xpath(xpath_1), "data-text");
}
return null;
}
这是我自己想到的方法,该方法有一个缺点,每次都要刷新一下网页,不然后会出现。
页:
[1]