如何使用多个CSS选择器使代码更具可读性

时间:2019-03-24 10:04:58

标签: python selenium

我正在寻找一种缩短代码的方法。

我正在从https://www.ultimateqa.com/complicated-page/收集验证码值,并将其提交给3个验证码框。

我可以使用函数来缩短代码吗? 还是应该使用其他CSS选择器?

for i in range(1):
    cap0 = '#et_pb_contact_form_0 > div.et_pb_contact > form > div > div > p > input'
    captext1= driver.find_element_by_css_selector(cap0).get_attribute("data-first_digit")
    capnumb1 = int(captext1)
    captext2= driver.find_element_by_css_selector(cap0).get_attribute("data-second_digit")
    capnumb2 = int(captext2)
    driver.find_element_by_css_selector(cap0).send_keys(capnumb1 + capnumb2)
for i in range(1):
    cap1 = '#et_pb_contact_form_1 > div.et_pb_contact > form > div > div > p > input'
    captext1= driver.find_element_by_css_selector(cap1).get_attribute("data-first_digit")
    capnumb1 = int(captext1)
    captext2= driver.find_element_by_css_selector(cap1).get_attribute("data-second_digit")
    capnumb2 = int(captext2)
    driver.find_element_by_css_selector(cap1).send_keys(capnumb1 + capnumb2)
for i in range(1):
    cap2 = '#et_pb_contact_form_2 > div.et_pb_contact > form > div > div > p > input'
    captext1= driver.find_element_by_css_selector(cap2).get_attribute("data-first_digit")
    capnumb1 = int(captext1)
    captext2= driver.find_element_by_css_selector(cap2).get_attribute("data-second_digit")
    capnumb2 = int(captext2)
    driver.find_element_by_css_selector(cap2).send_keys(capnumb1 + capnumb2)

3 个答案:

答案 0 :(得分:2)

您可以使用与所有3种情况匹配的选择器(用逗号分隔单独的CSS选择器)并循环处理它们:

cap_css = '#et_pb_contact_form_0 > div.et_pb_contact > form > div > div > p > input, #et_pb_contact_form_1 > div.et_pb_contact > form > div > div > p > input, #et_pb_contact_form_2 > div.et_pb_contact > form > div > div > p > input'
cap_list = driver.find_elements_by_css_selector(cap_css)
for cap in cap_list:
    captext1= cap.get_attribute("data-first_digit")
    capnumb1 = int(captext1)
    captext2= cap.get_attribute("data-second_digit")
    capnumb2 = int(captext2)
    cap.send_keys(capnumb1 + capnumb2)

P.S。 for i in range(1)循环应删除,因为它没有任何意义

答案 1 :(得分:2)

一些python /程序性注释:

  • 范围为(1)的for循环迭代一次(在第0个元素上);因此,它不是很有用,因为常规代码仍然会迭代一次。
  • 每当您看到类似的代码被写下来时,通常都可以将逻辑抽象为可重用的代码。

我个人不熟悉硒,但是对CSS不太了解,但是这是python的一个尝试:

def get_digit(cap,attr):
    element = driver.find_element_by_css_selector(cap).get_attribute(attr)
    return (element, int(element))

caps = ['#et_pb_contact_form_0', '#et_pb_contact_form_1', '#et_pb_contact_form_2']
cap_end = ' > div.et_pb_contact > form > div > div > p > input'
for cap in caps:
    tmp_cap = cap + cap_end
    captext1, capnumb1 = get_digit(tmp_cap, "data-first_digit")
    captext2, capnumb2 = get_digit(tmp_cap, "data-second_digit")
    driver.find_element_by_css_selector(tmp_cap).send_keys(capnumb1 + capnumb2)

在评论中让我知道这是否适合您!

将我的函数与JaSON的组合CSS选择器逻辑结合使用:

def get_digit(cap,attr):
    element = driver.find_element_by_css_selector(cap).get_attribute(attr)
    return (element, int(element))

cap_css = '#et_pb_contact_form_0 > div.et_pb_contact > form > div > div > p > input, #et_pb_contact_form_1 > div.et_pb_contact > form > div > div > p > input, #et_pb_contact_form_2 > div.et_pb_contact > form > div > div > p > input'
cap_list = driver.find_elements_by_css_selector(cap_css)
for cap in cap_list:
    captext1, capnumb1 = get_digit(cap, "data-first_digit")
    captext2, capnumb2 = get_digit(cap, "data-second_digit")
    cap.send_keys(capnumb1 + capnumb2)

答案 2 :(得分:-1)

您可以将name属性与循环索引一起用作参数

for i in range(3):
    cap = driver.find_element_by_css_selector(f'[name="et_pb_contact_captcha_{i}"]')
    captext = cap.get_attribute("data-first_digit")
    capnumb1 = int(captext)
    captext = cap.get_attribute("data-second_digit")
    capnumb2 = int(captext)
    cap.send_keys(capnumb + capnumb2)

或通过部分名称找到所有验证码,然后遍历列表

captchas = driver.find_elements_by_css_selector('[name^="et_pb_contact_captcha_"]')

for cap in captchas:
    captext = cap.get_attribute("data-first_digit")
    capnumb1 = int(captext)
    captext = cap.get_attribute("data-second_digit")
    capnumb2 = int(captext)
    cap.send_keys(capnumb + capnumb2)

无需每次都重新定位验证码字段,只需定位一次即可使用。