My requirement is this:
get all the elements of the tree list, and then check the corresponding check boxes according to the elements
1. Get the tree list
# 清空选项
driver.find_element_by_xpath(
"//*[@id='treeCountryGroup']//li[@class='level0']/span[starts-with(@class,'button chk checkbox_true')]").click()
time.sleep(3)
# 展开所有选项
driver.find_element_by_id('CGExpandAll').click()
time.sleep(2)
# 获取整个列表
country_list = driver.find_elements_by_xpath('//*[@id="treeCountryGroup"]') # 定位到整个树状结构列表元素
list_name = []
for item in country_list:
name = item.text
print(name)
list_name.append(name)
lists=list_name[0].split('\n')
print(lists)
print(len(lists))
2. Because the check box starts from 1, so I get the length of the list, and then form a dictionary with the tree list, and find the key according to the value of the dictionary
d=[]
for i in range(1,len(lists)):
d.append(i)
print(d)
f=zip(d,lists)
f=dict(f)
print(f)
f1={
v : k for k, v in f.items() }
print(f1)
3. Search the field, enter the value, and then check
driver.find_element_by_id('txtKeySearch').send_keys(‘ROI’)
time.sleep(1)
print(f1[c])
path='//*[@id="treeCountryGroup_10_check"]'
driver.find_element_by_xpath(path).click()
time.sleep(2)
4. xpath parameterization, with format the
following is all the code
def setting(c):
# 点击Setting
driver.switch_to.frame('ifContent')
driver.find_element_by_xpath('//*[@id="btnSetting"]').click()
time.sleep(2)
# 选择国家\清空选项
driver.find_element_by_xpath(
"//*[@id='treeCountryGroup']//li[@class='level0']/span[starts-with(@class,'button chk checkbox_true')]").click()
time.sleep(3)
# 展开所有选项
driver.find_element_by_id('CGExpandAll').click()
time.sleep(2)
# 获取整个列表
country_list = driver.find_elements_by_xpath('//*[@id="treeCountryGroup"]') # 定位到整个树状结构列表元素
list_name = []
for item in country_list:
name = item.text
print(name)
list_name.append(name)
lists=list_name[0].split('\n')
print(lists)
print(len(lists))
#获取树状列表长度,再和树状列表组成一个字典
d=[]
for i in range(1,len(lists)):
d.append(i)
print(d)
f=zip(d,lists)
f=dict(f)
print(f)
#字典的value和key 对调位置,通过value找key
f1={
v : k for k, v in f.items() }
print(f1)
#对xpath参数化
driver.find_element_by_id('txtKeySearch').send_keys(c)
time.sleep(1)
print(f1[c])
path='//*[@id="treeCountryGroup_{}_check"]'.format(f1[c])
driver.find_element_by_xpath(path).click()
time.sleep(2)
#方法调用
setting('ROI')
I wrote a lot of code, there should be a simpler way than this, just learn python, if there is a good way to update