我怎样才能使下面的代码更短呢?
if size == "6":
element = '#pdp__select-size > li:nth-child(1) > button'
if size == "8":
element = '#pdp__select-size > li:nth-child(2) > button'
if size == "10":
element = '#pdp__select-size > li:nth-child(3) > button'
if size == "12":
element = '#pdp__select-size > li:nth-child(4) > button'
if size == "14":
element = '#pdp__select-size > li:nth-child(5) > button'
if size == "16":
element = '#pdp__select-size > li:nth-child(6) > button'
if size == "18":
element = '#pdp__select-size > li:nth-child(7) > button'基本上,每次大小增加2(从6> 8)等等...我希望元素中的数字增加1(来自1>2),等等。
发布于 2021-11-10 17:34:44
将size转换为整数并执行一些算术运算,以获得nth()中的值。
size_int = int(size)
if size_int % 2 == 0 and 6 <= size_int <= 18:
# size is even
nth = size_int//2 - 2
element = f"#pdp__select-size > li:nth-child({nth}) > button"https://stackoverflow.com/questions/69917668
复制相似问题